aboutsummaryrefslogtreecommitdiffstats
path: root/static/modules/assistant.js
diff options
context:
space:
mode:
Diffstat (limited to 'static/modules/assistant.js')
-rw-r--r--static/modules/assistant.js78
1 files changed, 66 insertions, 12 deletions
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);