diff options
| -rw-r--r-- | static/deck.css | 11 | ||||
| -rw-r--r-- | static/modules/assistant.js | 78 |
2 files changed, 76 insertions, 13 deletions
diff --git a/static/deck.css b/static/deck.css index 0977279..1d02a85 100644 --- a/static/deck.css +++ b/static/deck.css @@ -517,7 +517,7 @@ select { background: var(--udt-surface0); border: 1px solid var(--udt-surface1); color: var(--udt-text); max-width: 50%; } .assistant .as-model span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } -.assistant .as-status { flex: 1; color: var(--udt-subtext0); font-size: 0.857rem; } +.assistant .as-gap { flex: 1; } /* pushes the mic right; the state is in the live bubble */ .assistant .as-models { position: absolute; top: 58px; left: 0; z-index: 2; max-height: 60%; overflow-y: auto; min-width: 50%; background: var(--udt-mantle); border: 1px solid var(--udt-surface1); border-radius: 12px; padding: 6px; @@ -576,6 +576,15 @@ select { .assistant .as-bubble .md > :first-child { margin-top: 0; } .assistant .as-bubble .md > :last-child { margin-bottom: 0; } .assistant .as-tools { font-size: 0.786rem; color: var(--udt-overlay0); } +.assistant .as-state { font-size: 0.786rem; font-style: italic; color: var(--udt-subtext0); } +.assistant .as-state:empty { display: none; } +/* Three dots bouncing in turn while the reply has no words yet. */ +.assistant .as-dots { display: inline-flex; gap: 5px; padding: 6px 0 2px; } +.assistant .as-dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--udt-subtext0); + animation: as-bounce 1.2s ease-in-out infinite; } +.assistant .as-dots i:nth-child(2) { animation-delay: .15s; } +.assistant .as-dots i:nth-child(3) { animation-delay: .3s; } +@keyframes as-bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-6px); opacity: 1; } } .assistant .md { line-height: 1.45; overflow-wrap: anywhere; } .assistant .md pre { position: relative; background: var(--udt-crust); border-radius: 8px; padding: 10px; overflow-x: auto; } .assistant .md pre .copy { position: absolute; top: 4px; right: 4px; } diff --git a/static/modules/assistant.js b/static/modules/assistant.js index e47fe4b..2f86e57 100644 --- a/static/modules/assistant.js +++ b/static/modules/assistant.js @@ -68,8 +68,9 @@ function addCopy(box) { } // One exchange as chat bubbles: the question on the right with the user's -// avatar, the reply on the left with the assistant's; "…" while a reply has -// no text yet. +// avatar, the reply on the left with the assistant's. A live reply also has +// a state line (thinking, transcribing, a tool) and bouncing dots until its +// first words. function turnNode(t, live) { const d = document.createElement("div"); d.className = "as-turn"; @@ -84,6 +85,11 @@ function turnNode(t, live) { row.className = "as-row bot"; row.innerHTML = `<div class="as-av bot"></div><div class="as-bubble"></div>`; const bubble = row.lastElementChild; + if (live) { + const state = document.createElement("div"); + state.className = "as-state"; + bubble.append(state); + } if (t.tools?.length) { const tools = document.createElement("div"); tools.className = "as-tools"; @@ -93,7 +99,7 @@ function turnNode(t, live) { const a = document.createElement("div"); a.className = "as-a md"; if (t.reply) a.innerHTML = render(t.reply); - else if (live) a.textContent = "…"; + else if (live) a.innerHTML = DOTS; if (!live) addCopy(a); bubble.append(a); if (t.error && t.error !== "stopped") { @@ -123,18 +129,68 @@ function faceStyle(el, face) { img.src = "/api/bg"; } +const DOTS = `<span class="as-dots"><i></i><i></i><i></i></span>`; + +// The live reply types out between polls instead of landing in 0.5 s +// chunks: it catches up with what has arrived within about CATCH_UP seconds, +// and the Markdown is rendered at most every RENDER_MS. +const CATCH_UP = 0.6; +const RENDER_MS = 50; +function typeOut(live, log, el) { + if (live._raf) return; + let last = performance.now(), drawn = 0; + const a = live.querySelector(".as-a"); + const step = (now) => { + const target = live._target; + if (!live.isConnected || live._shown >= target.length) { live._raf = null; return; } + const rate = Math.max(40, (target.length - live._shown) / CATCH_UP); // chars per second + live._shown = Math.min(target.length, live._shown + Math.max(1, Math.round((rate * (now - last)) / 1000))); + last = now; + if (now - drawn >= RENDER_MS || live._shown === target.length) { + drawn = now; + a.innerHTML = render(target.slice(0, live._shown)); + if (el._stick) log.scrollTop = log.scrollHeight; + } + live._raf = requestAnimationFrame(step); + }; + live._raf = requestAnimationFrame(step); +} + function renderLog(log, st, el) { if (el._epoch !== st.epoch || el._done > st.turns.length) { log.textContent = ""; el._epoch = st.epoch; el._done = 0; } - log.querySelector(".as-live")?.remove(); - for (; el._done < st.turns.length; el._done++) log.append(turnNode(st.turns[el._done], false)); - if (st.pending) { - const live = turnNode(st.pending, true); - live.classList.add("as-live"); - log.append(live); + let live = log.querySelector(".as-live"); + for (; el._done < st.turns.length; el._done++) log.insertBefore(turnNode(st.turns[el._done], false), live); + // The live turn is kept and updated in place, so its dots keep bouncing + // and its text keeps typing; a new question starts a new one. + const p = st.pending ?? (st.state !== "idle" ? {} : null); + if (live && (!p || live._user !== (p.user ?? ""))) { + live.remove(); + live = null; + } + if (p) { + if (!live) { + live = turnNode({ user: p.user }, true); + live.classList.add("as-live"); + live._user = p.user ?? ""; + live._shown = 0; + log.append(live); + } + live.querySelector(".as-state").textContent = + st.state === "tool" ? `running ${st.tool}…` : STATUS[st.state] ?? ""; + const tools = (p.tools ?? []).join(" · "); + let line = live.querySelector(".as-tools"); + if (tools && !line) { + line = document.createElement("div"); + line.className = "as-tools"; + live.querySelector(".as-state").after(line); + } + if (line) line.textContent = tools; + live._target = p.reply ?? ""; + if (live._target) typeOut(live, log, el); } if (el._stick) log.scrollTop = log.scrollHeight; } @@ -314,7 +370,7 @@ export default { </div> <div class="as-log"></div> <div class="as-notice"></div> - <div class="as-bar"><span class="as-status"></span></div> + <div class="as-bar"><span class="as-gap"></span></div> </div> </div>`; const head = el.querySelector(".as-head"); @@ -371,8 +427,6 @@ export default { const cloud = st.models.some((m) => m.cloud && m.id === st.wanted); model.querySelector("i").textContent = cloud ? G.cloud : ""; model.querySelector("span").textContent = st.wanted || st.model || "model"; - el.querySelector(".as-status").textContent = - st.state === "tool" ? `running ${st.tool}…` : STATUS[st.state] ?? ""; const panel = el.querySelector(".as-models"); if (!panel.hidden) renderModels(panel, st, el); |
