"use strict"; const $ = (id) => document.getElementById(id); const API_BASE = ((window.APP_CONFIG && window.APP_CONFIG.API_BASE) || "").replace(/\/+$/, ""); const KEY_STORE = "demoKey"; let threadId = null; let lastStatus = null; let busyNow = false; const PLACEHOLDERS = { awaiting_answers: "Answer the questions, e.g. Alzheimer's, service-line, none", awaiting_approval: "Approve or reject the draft above", done: "Ask a follow-up question about this briefing", rejected: "Ask a follow-up question about this briefing", }; /* ---------- storage helpers ---------- */ function getKey() { try { return sessionStorage.getItem(KEY_STORE) || ""; } catch (e) { return ""; } } function setKey(v) { try { if (v) sessionStorage.setItem(KEY_STORE, v); else sessionStorage.removeItem(KEY_STORE); } catch (e) { /* ignore */ } } /* ---------- DOM helpers ---------- */ function el(tag, cls, text) { const n = document.createElement(tag); if (cls) n.className = cls; if (text != null) n.textContent = text; return n; } const SVGNS = "http://www.w3.org/2000/svg"; function icon(name) { const s = document.createElementNS(SVGNS, "svg"); s.setAttribute("class", "ic"); s.setAttribute("aria-hidden", "true"); const u = document.createElementNS(SVGNS, "use"); u.setAttribute("href", `#i-${name}`); s.appendChild(u); return s; } function toast(msg, isErr) { const t = el("div", "toast" + (isErr ? " err" : ""), msg); $("toasts").appendChild(t); setTimeout(() => t.remove(), 4500); } /* ---------- theme ---------- */ function currentTheme() { const t = document.documentElement.getAttribute("data-theme"); if (t) return t; return window.matchMedia && matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"; } function syncThemeIcon() { const dark = currentTheme() === "dark"; $("theme-ic").firstElementChild.setAttribute("href", dark ? "#i-sun" : "#i-moon"); $("theme").setAttribute("aria-label", dark ? "Switch to light mode" : "Switch to dark mode"); } $("theme").addEventListener("click", () => { const next = currentTheme() === "dark" ? "light" : "dark"; document.documentElement.setAttribute("data-theme", next); try { localStorage.setItem("theme", next); } catch (e) { /* ignore */ } syncThemeIcon(); }); syncThemeIcon(); /* ---------- markdown (escape first; only https links become anchors) ---------- */ function escapeHtml(s) { return String(s).replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'"); } function chipKind(t) { if (/^PMID:/i.test(t)) return "pmid"; if (/^NCT\d/i.test(t)) return "nct"; if (/^FDA:/i.test(t)) return "fda"; if (/^DOC:/i.test(t)) return "doc"; return null; } const CITE_RE = /^(PMID:\S+|NCT\d+|FDA:\S+|DOC:\S+)$/i; function inline(s) { return escapeHtml(s) .replace(/\[([^\]]+)\]\((https:\/\/[^\s)]+)\)/g, (_, t, u) => { const k = CITE_RE.test(t) ? chipKind(t) : null; return `${t}`; }) .replace(/`([^`]+)`/g, (_, c) => { const k = CITE_RE.test(c) ? chipKind(c) : null; return k ? `${c}` : `${c}`; }) .replace(/\*\*([^*]+)\*\*/g, "$1") .replace(/~~([^~]+)~~/g, "$1") .replace(/(^|\s)_([^_]+)_(?=\s|$|[.,;:])/g, "$1$2"); } function renderMarkdown(md) { const lines = String(md).split("\n"); let html = "", list = null, table = []; const flushList = () => { if (list) { html += ``; list = null; } }; const flushTable = () => { if (!table.length) return; const rows = table.filter((r) => !/^\|\s*:?-/.test(r)).map((r) => r.replace(/^\||\|$/g, "").split("|")); html += '
' + rows[0].map((c) => ``).join("") + "" + rows.slice(1).map((cells) => "" + cells.map((c) => ``).join("") + "").join("") + "
${inline(c.trim())}
${inline(c.trim())}
"; table = []; }; for (const raw of lines) { const line = raw.trimEnd(); if (line.startsWith("|")) { flushList(); table.push(line); continue; } flushTable(); let m; if ((m = line.match(/^(#{1,3}) (.*)/))) { flushList(); html += `${inline(m[2])}`; } else if ((m = line.match(/^- (.*)/))) { if (list !== "ul") { flushList(); html += "