/* ═══════════════════════════════════════════════════════════════ Stacklogix — Chat Client (Vanilla JS) ═══════════════════════════════════════════════════════════════ */ const API_BASE = window.location.origin; // ─── State ──────────────────────────────────────────────────── let currentSessionId = null; let isWaiting = false; // ─── DOM refs ───────────────────────────────────────────────── const $messages = document.getElementById("messages"); const $msgContainer = document.getElementById("messagesContainer"); const $input = document.getElementById("userInput"); const $btnSend = document.getElementById("btnSend"); const $btnNewChat = document.getElementById("btnNewChat"); const $btnToggle = document.getElementById("btnToggleSidebar"); const $sidebar = document.getElementById("sidebar"); const $sessionList = document.getElementById("sessionList"); const $welcomeScreen = document.getElementById("welcomeScreen"); // ─── UUID generator ─────────────────────────────────────────── function uuid() { return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, c => { const r = (Math.random() * 16) | 0; return (c === "x" ? r : (r & 0x3) | 0x8).toString(16); }); } // ─── Simple markdown → HTML ─────────────────────────────────── function renderMarkdown(text) { if (!text) return ""; let html = text // Code blocks .replace(/```(\w*)\n([\s\S]*?)```/g, '
$2
') // Inline code .replace(/`([^`]+)`/g, '$1') // Bold .replace(/\*\*(.+?)\*\*/g, '$1') // Italic .replace(/\*(.+?)\*/g, '$1') // Headers .replace(/^### (.+)$/gm, '

$1

') .replace(/^## (.+)$/gm, '

$1

') .replace(/^# (.+)$/gm, '

$1

') // Blockquote .replace(/^> (.+)$/gm, '
$1
') // Unordered list items .replace(/^[-*] (.+)$/gm, '
  • $1
  • ') // Numbered list items .replace(/^\d+\. (.+)$/gm, '
  • $1
  • '); // Wrap consecutive
  • in