aboutsummaryrefslogtreecommitdiffstats
path: root/static/modules
diff options
context:
space:
mode:
Diffstat (limited to 'static/modules')
-rw-r--r--static/modules/assistant.js44
1 files changed, 36 insertions, 8 deletions
diff --git a/static/modules/assistant.js b/static/modules/assistant.js
index 0384dc5..3213aa6 100644
--- a/static/modules/assistant.js
+++ b/static/modules/assistant.js
@@ -67,35 +67,62 @@ 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.
function turnNode(t, live) {
const d = document.createElement("div");
d.className = "as-turn";
if (t.user) {
- const q = document.createElement("div");
- q.className = "as-q";
- q.textContent = t.user;
- d.append(q);
+ const row = document.createElement("div");
+ row.className = "as-row me";
+ row.innerHTML = `<div class="as-q"></div><div class="as-av me"></div>`;
+ row.firstElementChild.textContent = t.user;
+ d.append(row);
}
+ const row = document.createElement("div");
+ row.className = "as-row bot";
+ row.innerHTML = `<div class="as-av bot"></div><div class="as-bubble"></div>`;
+ const bubble = row.lastElementChild;
if (t.tools?.length) {
const tools = document.createElement("div");
tools.className = "as-tools";
tools.textContent = t.tools.join(" · ");
- d.append(tools);
+ bubble.append(tools);
}
const a = document.createElement("div");
a.className = "as-a md";
- a.innerHTML = render(t.reply);
+ if (t.reply) a.innerHTML = render(t.reply);
+ else if (live) a.textContent = "…";
if (!live) addCopy(a);
- d.append(a);
+ bubble.append(a);
if (t.error && t.error !== "stopped") {
const e = document.createElement("div");
e.className = "as-err bad";
e.textContent = t.error;
- d.append(e);
+ bubble.append(e);
}
+ d.append(row);
return d;
}
+// The assistant's avatar is the background picture zoomed onto the face
+// ([assistant] face); its real size is needed to scale it, so it is loaded once.
+function faceStyle(el, face) {
+ if (!face || el._face) return;
+ el._face = true;
+ const img = new Image();
+ img.onload = () => {
+ const [x, y, size] = face;
+ const k = 40 / size; // the avatar is 40px
+ const box = el.querySelector(".assistant");
+ box.style.setProperty("--face-size", `${img.naturalWidth * k}px ${img.naturalHeight * k}px`);
+ box.style.setProperty("--face-pos", `${-(x - size / 2) * k}px ${-(y - size / 2) * k}px`);
+ box.classList.add("has-face");
+ };
+ img.src = "/api/bg";
+}
+
function renderLog(log, st, el) {
if (el._epoch !== st.epoch || el._done > st.turns.length) {
log.textContent = "";
@@ -320,6 +347,7 @@ export default {
off.textContent = "";
off._offline = null;
+ faceStyle(el, st.face);
el._busy = !!st.pending || st.state !== "idle";
// The backdrop shows more while the chat is empty.
el.querySelector(".assistant").classList.toggle("idle", !st.turns.length && !st.pending);