Download ui/web/src/components/chat/MessageList.svelte from localailb/assistant: direct link, hf CLI and curl.
- Browser
- Download file 16.3 kB
-
https://huggingface.co/spaces/localailb/assistant/resolve/main/ui/web/src/components/chat/MessageList.svelte
- Command line
-
hf download hf://spaces/localailb/assistant/ui/web/src/components/chat/MessageList.svelte
-
curl -L -o MessageList.svelte https://huggingface.co/spaces/localailb/assistant/resolve/main/ui/web/src/components/chat/MessageList.svelte
16.3 kB
| <script> | |
| import { app, t } from "../../lib/stores.svelte.js"; | |
| import "./MessageList.css"; | |
| import { enhanceRichHtml, renderMarkdown } from "../../lib/enhance.js"; | |
| import { tabEmoji, tabEmptyKey } from "../../lib/chatTabs.js"; | |
| /** Messages (already including the optimistic pending echo), busy flag, | |
| * the tab key (for the empty-state emoji/label), the streamed terminal | |
| * log lines (from the backend run) and the live streamed model tokens | |
| * (from the backend's `live` WS events). Read-only props. */ | |
| let { tab, messages, busy, runLog = [], liveText = "" } = $props(); | |
| let scroller; | |
| /** Index of the message whose copy button currently shows "Copied". */ | |
| let copied = $state(null); | |
| let copiedTimer = null; | |
| /** Active run elapsed seconds. */ | |
| let elapsed = $state(0); | |
| const downloadInfo = $derived.by(() => { | |
| if (app.modelDl.running) { | |
| const prog = app.modelDl.progress || ""; | |
| const pctMatch = /(\d+)%/.exec(prog); | |
| const pct = pctMatch ? parseInt(pctMatch[1], 10) : 0; | |
| return { active: true, text: prog || t("model_dl_progress").replace("{progress}", ""), percent: pct }; | |
| } | |
| for (let i = runLog.length - 1; i >= 0; i--) { | |
| const line = runLog[i]; | |
| if (typeof line !== "string") continue; | |
| if (/downloading/i.test(line) || /fetching.*files/i.test(line) || /hf_hub_download/i.test(line) || /model_download/i.test(line) || /snapshot_download/i.test(line)) { | |
| const clean = line.replace(/^\[.*?\]\s*/, ""); | |
| const pctMatch = /(\d+)%/.exec(clean); | |
| const pct = pctMatch ? parseInt(pctMatch[1], 10) : 0; | |
| return { active: true, text: clean, percent: pct }; | |
| } | |
| } | |
| return { active: false, text: "", percent: 0 }; | |
| }); | |
| /** Full-size chart viewer: { src, alt } of the chart being zoomed, or | |
| * null when closed. Clicking a .chart-card image opens it (event | |
| * delegation on the messages container — the gallery HTML is | |
| * server-rendered); the overlay, the ✕ button or Escape closes it. */ | |
| let lightbox = $state(null); | |
| function openChart(e) { | |
| const img = e.target.closest?.(".chart-card img"); | |
| if (!img) return; | |
| e.preventDefault(); | |
| const caption = | |
| img.closest(".chart-card")?.querySelector("figcaption")?.textContent || | |
| img.getAttribute("alt") || | |
| ""; | |
| lightbox = { src: img.currentSrc || img.src, alt: caption }; | |
| } | |
| function closeLightbox() { | |
| lightbox = null; | |
| } | |
| // Escape closes the lightbox; the listener lives only while it is open. | |
| $effect(() => { | |
| if (!lightbox) return; | |
| const onKey = (e) => { | |
| if (e.key === "Escape") lightbox = null; | |
| }; | |
| window.addEventListener("keydown", onKey); | |
| return () => window.removeEventListener("keydown", onKey); | |
| }); | |
| $effect(() => { | |
| let timer = null; | |
| if (busy) { | |
| elapsed = 0; | |
| timer = setInterval(() => { | |
| elapsed += 1; | |
| }, 1000); | |
| } else { | |
| elapsed = 0; | |
| } | |
| return () => { | |
| if (timer) clearInterval(timer); | |
| }; | |
| }); | |
| /** Thinking/status bubbles (🤖 / 📚 / 🔬 / 🖼️ / 🌐 / 📊 — optionally 🌐🤖 | |
| * or 🖼️ with its U+FE0F variation selector) are short placeholders. The | |
| * `u` flag is REQUIRED: without it a character class matches a single | |
| * UTF-16 code unit, so `[🤖]\s` demanded whitespace after the emoji's | |
| * first surrogate and NEVER matched — the pulse/status-card branches were | |
| * silently dead and every status bubble fell through to the .rich render. */ | |
| const isThinking = (m) => | |
| /^[🤖📚🔬🖼️🌐📊][🤖]?\uFE0F?\s/u.test(m.content || "") && | |
| (m.content || "").length < 140; | |
| function isStepMessage(m) { | |
| if (!m || m.role !== "assistant") return false; | |
| const c = m.content || ""; | |
| return c.includes("agent-step-details") || c.includes("agent-plan-box") || isThinking(m); | |
| } | |
| function getStepCount(msgs) { | |
| let count = 0; | |
| for (const m of msgs) { | |
| if (m.role === "assistant" && typeof m.content === "string") { | |
| if (m.content.includes("agent-step-details") || m.content.includes("agent-plan-box")) { | |
| count += 1; | |
| } | |
| } | |
| } | |
| return count; | |
| } | |
| function getStatusTitle(tabKey, sec) { | |
| if (sec < 5) { | |
| if (tabKey === "rag") return t("web_status_init_rag"); | |
| if (tabKey === "deep_research") return t("web_status_init_dr"); | |
| if (tabKey === "vision") return t("web_status_init_vis"); | |
| return t("web_status_init_gen"); | |
| } | |
| if (sec < 20) { | |
| return t("web_status_reasoning"); | |
| } | |
| return t("web_status_computing"); | |
| } | |
| function getStatusDetail(tabKey, sec) { | |
| if (sec < 20) { | |
| return t("web_status_reasoning_detail"); | |
| } | |
| return t("web_status_slow_warn"); | |
| } | |
| function getStepWorkingTitle(tabKey, stepCount, sec) { | |
| const nextStep = Math.max(1, stepCount + 1); | |
| const template = t("web_status_step_working"); | |
| return template ? template.replace("{step}", nextStep) : `⚙️ Working on Step ${nextStep}…`; | |
| } | |
| function getStepWorkingDetail(tabKey, stepCount, sec) { | |
| if (sec >= 30) { | |
| return t("web_status_slow_warn"); | |
| } | |
| return t("web_status_step_detail"); | |
| } | |
| /** execCommand fallback for environments without the async clipboard API. */ | |
| function legacyCopy(text) { | |
| const ta = document.createElement("textarea"); | |
| ta.value = text; | |
| ta.style.position = "fixed"; | |
| ta.style.opacity = "0"; | |
| document.body.appendChild(ta); | |
| ta.select(); | |
| document.execCommand("copy"); | |
| ta.remove(); | |
| } | |
| /** Copy the message's VISIBLE text (innerText — never the raw | |
| * markdown/HTML) to the clipboard, then flash the button as "Copied". | |
| * Assistant answers prefer the .final-answer-block so the copy is the | |
| * answer itself, not the 🧠 thinking summary or the model/timing footer. | |
| * Runs from a click, so the clipboard permission grant applies. */ | |
| function copyMessage(e, i) { | |
| const msgEl = e.currentTarget.closest(".msg"); | |
| const textEl = | |
| msgEl?.querySelector(".user-bubble") || | |
| msgEl?.querySelector(".final-answer-block") || | |
| msgEl?.querySelector(".rich"); | |
| const text = (textEl?.innerText || "").trim(); | |
| if (!text) return; | |
| const done = () => { | |
| copied = i; | |
| if (copiedTimer) clearTimeout(copiedTimer); | |
| copiedTimer = setTimeout(() => { | |
| if (copied === i) copied = null; | |
| }, 1600); | |
| }; | |
| if (navigator.clipboard?.writeText) { | |
| navigator.clipboard.writeText(text).then(done).catch(() => { | |
| try { | |
| legacyCopy(text); | |
| done(); | |
| } catch { | |
| /* ignore — nothing to copy */ | |
| } | |
| }); | |
| } else { | |
| try { | |
| legacyCopy(text); | |
| done(); | |
| } catch { | |
| /* ignore */ | |
| } | |
| } | |
| } | |
| /** The last message pulses while a run is in flight. */ | |
| const isLive = (i) => busy && i === messages.length - 1; | |
| // Keep the newest message in view: snap while busy (streaming), smooth-scroll | |
| // when the run finishes. | |
| $effect(() => { | |
| messages; | |
| busy; | |
| elapsed; | |
| liveText; | |
| requestAnimationFrame(() => { | |
| if (scroller) { | |
| scroller.scrollTo({ | |
| top: scroller.scrollHeight, | |
| behavior: busy ? "auto" : "smooth", | |
| }); | |
| } | |
| }); | |
| }); | |
| </script> | |
| <div class="messages" bind:this={scroller} onclick={openChart}> | |
| <div class="messages-inner"> | |
| {#if messages.length === 0 && !busy} | |
| <div class="empty"> | |
| <div class="empty-mark">{tabEmoji(tab)}</div> | |
| <div class="empty-title">{t(tabEmptyKey(tab))}</div> | |
| <div>{t("web_empty_hint")}</div> | |
| </div> | |
| {/if} | |
| {#each messages as m, i (i)} | |
| {#if m.role === "user"} | |
| <div class="msg msg-user"> | |
| <button | |
| type="button" | |
| class="msg-copy-btn" | |
| class:copied={copied === i} | |
| data-tooltip={copied === i ? t("web_copied") : t("web_copy")} | |
| aria-label={copied === i ? t("web_copied") : t("web_copy")} | |
| onclick={(e) => copyMessage(e, i)} | |
| > | |
| {#if copied === i} | |
| <svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="20 6 9 17 4 12"></polyline></svg> | |
| {:else} | |
| <svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg> | |
| {/if} | |
| </button> | |
| <div class="user-bubble">{m.content}</div> | |
| </div> | |
| {:else} | |
| <div class="msg msg-ai" class:live={isLive(i)}> | |
| <div class="ai-bubble"> | |
| {#if isThinking(m)} | |
| <div class="think-pulse"> | |
| {m.content} | |
| <span class="dots"><span></span><span></span><span></span></span> | |
| {#if busy} | |
| <span class="status-timer">⏱️ {elapsed}s</span> | |
| {/if} | |
| </div> | |
| {:else} | |
| <div class="rich">{@html enhanceRichHtml(m.content)}</div> | |
| {/if} | |
| </div> | |
| {#if !isThinking(m)} | |
| <button | |
| type="button" | |
| class="msg-copy-btn" | |
| class:copied={copied === i} | |
| data-tooltip={copied === i ? t("web_copied") : t("web_copy")} | |
| aria-label={copied === i ? t("web_copied") : t("web_copy")} | |
| onclick={(e) => copyMessage(e, i)} | |
| > | |
| {#if copied === i} | |
| <svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="20 6 9 17 4 12"></polyline></svg> | |
| {:else} | |
| <svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg> | |
| {/if} | |
| </button> | |
| {/if} | |
| </div> | |
| {/if} | |
| {/each} | |
| {#if busy && !liveText && (messages.length === 0 || messages[messages.length - 1].role === "user")} | |
| <div class="msg msg-ai live"> | |
| <div class="ai-bubble"> | |
| <div class="active-status-card"> | |
| <div class="status-header"> | |
| <div class="status-pulse-dot"></div> | |
| <span class="status-title">{getStatusTitle(tab, elapsed)}</span> | |
| <span class="status-timer">⏱️ {elapsed}s</span> | |
| </div> | |
| <div class="status-detail">{getStatusDetail(tab, elapsed)}</div> | |
| {#if elapsed >= 30} | |
| <div class="status-slow-hint"> | |
| {elapsed >= 60 ? t("web_status_stalled_warn") : t("web_status_slow_warn")} | |
| </div> | |
| {/if} | |
| {#if downloadInfo.active} | |
| <div class="model-dl-progress-card"> | |
| <div class="model-dl-progress-header"> | |
| <span>📥 {downloadInfo.text}</span> | |
| {#if downloadInfo.percent > 0} | |
| <span>{downloadInfo.percent}%</span> | |
| {/if} | |
| </div> | |
| <div class="model-dl-progress-track"> | |
| <div | |
| class="model-dl-progress-fill" | |
| class:indeterminate={downloadInfo.percent <= 0} | |
| style={downloadInfo.percent > 0 ? `width: ${downloadInfo.percent}%;` : ""} | |
| ></div> | |
| </div> | |
| </div> | |
| {/if} | |
| {#if runLog.length > 0 && getStepCount(messages) === 0} | |
| <!-- Construction lines ([DataAgent]/[Model]/[llama-server] …) | |
| belong to the INITIAL phase only: shown live while the | |
| model loads / agents build, and hidden the moment the | |
| first real step starts (getStepCount > 0) — the card then | |
| shows just "Working on Step N" for the rest of the run. --> | |
| <div class="run-lines"> | |
| {#each runLog as line (line)} | |
| <div class="run-line">{line}</div> | |
| {/each} | |
| </div> | |
| {/if} | |
| </div> | |
| </div> | |
| </div> | |
| {:else if busy && !liveText && messages.length > 0 && messages[messages.length - 1].role === "assistant" && isStepMessage(messages[messages.length - 1])} | |
| <div class="msg msg-ai live"> | |
| <div class="ai-bubble"> | |
| <div class="active-status-card active-step-card"> | |
| <div class="status-header"> | |
| <div class="status-pulse-dot"></div> | |
| <span class="status-title">{getStepWorkingTitle(tab, getStepCount(messages), elapsed)}</span> | |
| <span class="status-timer">⏱️ {elapsed}s</span> | |
| </div> | |
| <div class="status-detail">{getStepWorkingDetail(tab, getStepCount(messages), elapsed)}</div> | |
| {#if elapsed >= 30} | |
| <div class="status-slow-hint"> | |
| {elapsed >= 60 ? t("web_status_stalled_warn") : t("web_status_slow_warn")} | |
| </div> | |
| {/if} | |
| {#if downloadInfo.active} | |
| <div class="model-dl-progress-card"> | |
| <div class="model-dl-progress-header"> | |
| <span>📥 {downloadInfo.text}</span> | |
| {#if downloadInfo.percent > 0} | |
| <span>{downloadInfo.percent}%</span> | |
| {/if} | |
| </div> | |
| <div class="model-dl-progress-track"> | |
| <div | |
| class="model-dl-progress-fill" | |
| class:indeterminate={downloadInfo.percent <= 0} | |
| style={downloadInfo.percent > 0 ? `width: ${downloadInfo.percent}%;` : ""} | |
| ></div> | |
| </div> | |
| </div> | |
| {/if} | |
| {#if runLog.length > 0 && getStepCount(messages) === 0} | |
| <div class="run-lines"> | |
| {#each runLog as line (line)} | |
| <div class="run-line">{line}</div> | |
| {/each} | |
| </div> | |
| {/if} | |
| </div> | |
| </div> | |
| </div> | |
| {/if} | |
| {#if busy && liveText} | |
| <!-- The model's ACTUAL tokens streamed live (`live` WS events from the | |
| backend's live_stream bus). While non-empty this replaces the | |
| status card above — the answer is being written in real time. | |
| Rendered through the same marked pipeline as final answers (the | |
| `rich` class) so headings / lists / **bold** show formatted | |
| instead of leaking literal `#` / `*` characters. The compact | |
| header (pulse + bilingual label + timer) keeps the "live" feel | |
| readable: it is clearly the model's own output, still streaming. --> | |
| <div class="msg msg-ai live"> | |
| <div class="ai-bubble"> | |
| <div class="live-streaming-head"> | |
| <div class="status-pulse-dot"></div> | |
| <span class="live-streaming-label">{t("web_live_streaming")}</span> | |
| <span class="status-timer">⏱️ {elapsed}s</span> | |
| </div> | |
| <div class="live-text-block rich">{@html renderMarkdown(liveText)}</div> | |
| </div> | |
| </div> | |
| {/if} | |
| </div> | |
| {#if lightbox} | |
| <!-- Chart zoom: the whole overlay closes on a background click; the | |
| image itself stays put so it can be inspected. --> | |
| <div | |
| class="chart-lightbox" | |
| role="dialog" | |
| aria-modal="true" | |
| onclick={(e) => { | |
| if (e.target === e.currentTarget) closeLightbox(); | |
| }} | |
| > | |
| <button | |
| type="button" | |
| class="lightbox-close" | |
| aria-label={t("web_tt_close_chart")} | |
| data-tooltip={t("web_tt_close_chart")} | |
| onclick={closeLightbox} | |
| > | |
| × | |
| </button> | |
| <img src={lightbox.src} alt={lightbox.alt} /> | |
| <span class="lightbox-caption">{lightbox.alt}</span> | |
| </div> | |
| {/if} | |
| </div> | |