File size: 16,265 Bytes
bc9f3e6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | <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>
|