diff options
| -rw-r--r-- | config.toml | 5 | ||||
| -rw-r--r-- | deckit/modules/assistant.py | 4 | ||||
| -rw-r--r-- | deckit/server.py | 14 | ||||
| -rw-r--r-- | static/deck.css | 22 | ||||
| -rw-r--r-- | static/modules/assistant.js | 44 | ||||
| -rw-r--r-- | tests/test_server.py | 2 |
6 files changed, 77 insertions, 14 deletions
diff --git a/config.toml b/config.toml index 39fc22c..f32b3f5 100644 --- a/config.toml +++ b/config.toml @@ -77,6 +77,11 @@ shell = "~/Programming/GIT/quickshell/desktop" # An image blended faintly into the Assistant tab's background, its subject # rising from the bottom-right corner. Empty: none. background = "" +# Chat avatars: the user's picture, and where the assistant's face is in the +# background image, as [centre x, centre y, size] in its pixels. Empty: plain +# circles. +user_avatar = "" +face = [] [windows] # keystone's workspace icons (its bar and Alt+Tab show these): a QML map of diff --git a/deckit/modules/assistant.py b/deckit/modules/assistant.py index f0b3947..4355cfa 100644 --- a/deckit/modules/assistant.py +++ b/deckit/modules/assistant.py @@ -127,6 +127,9 @@ def qarma_args(item, kinds): def make(cfg: dict) -> Module: shell = os.path.expanduser(cfg.get("assistant", {}).get("shell", "")) + face = cfg.get("assistant", {}).get("face") or None + if not (isinstance(face, list) and len(face) == 3 and all(isinstance(v, (int, float)) for v in face)): + face = None # [centre x, centre y, size] in the background image, else a plain circle lock = threading.Lock() # guards st send_lock = threading.Lock() # one writer on the socket at a time voice_lock = threading.Lock() @@ -201,6 +204,7 @@ def make(cfg: dict) -> Module: out = json.loads(json.dumps(st)) # a copy: the reader keeps writing out["wanted"] = wanted() out["hears"] = hears(st["models"], out["wanted"]) + out["face"] = face return out def model(body): diff --git a/deckit/server.py b/deckit/server.py index 9dd5229..f3e1976 100644 --- a/deckit/server.py +++ b/deckit/server.py @@ -112,9 +112,13 @@ def art(): return None -def background(): - """The Assistant tab's backdrop, `[assistant] background`; None when unset.""" - name = CFG.get("assistant", {}).get("background", "") +# /api/<route> -> the [assistant] key naming a local image to serve there. +CONFIG_IMAGES = {"bg": "background", "avatar": "user_avatar"} + + +def config_image(key): + """The Assistant tab's backdrop or the user's avatar; None when unset.""" + name = CFG.get("assistant", {}).get(key, "") path = Path(name).expanduser() try: return (path.read_bytes(), mimetypes.guess_type(path)[0] or "image/jpeg") if name else None @@ -208,8 +212,8 @@ class Handler(BaseHTTPRequestHandler): if len(parts) == 2 and parts[1] == "art": image = art() return self._send(200, image[0], image[1], cache=True) if image else self._send(404) - if len(parts) == 2 and parts[1] == "bg": - image = background() + if len(parts) == 2 and parts[1] in CONFIG_IMAGES: + image = config_image(CONFIG_IMAGES[parts[1]]) return self._send(200, image[0], image[1], cache=True) if image else self._send(404) if len(parts) == 4 and parts[2] == "image": try: diff --git a/static/deck.css b/static/deck.css index e60c732..48ec626 100644 --- a/static/deck.css +++ b/static/deck.css @@ -674,7 +674,27 @@ select { .assistant .as-op, .assistant .as-notice, .assistant .as-note { font-size: 0.857rem; color: var(--udt-subtext0); } .assistant .as-notice:empty { display: none; } .assistant .as-log { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; padding-right: 4px; } -.assistant .as-q { align-self: flex-end; background: var(--udt-surface1); border-radius: 12px; padding: 8px 12px; white-space: pre-wrap; margin-left: 20%; } +.assistant .as-turn { display: flex; flex-direction: column; gap: 10px; } +.assistant .as-row { display: flex; align-items: flex-end; gap: 10px; } +.assistant .as-row.me { justify-content: flex-end; padding-left: 15%; } +.assistant .as-row.bot { padding-right: 10%; } +.assistant .as-av { + flex: none; width: 40px; height: 40px; border-radius: 20px; + background: var(--udt-surface1) center / cover no-repeat; +} +.assistant .as-av.me { background-image: url("/api/avatar"); } +.assistant.has-face .as-av.bot { background-image: url("/api/bg"); background-size: var(--face-size); background-position: var(--face-pos); } +.assistant .as-q, .assistant .as-bubble { padding: 10px 14px; border-radius: 16px; min-width: 0; } +.assistant .as-q { + white-space: pre-wrap; color: var(--udt-crust); + background: color-mix(in srgb, var(--udt-accent) 85%, var(--udt-text)); border-bottom-right-radius: 4px; +} +.assistant .as-bubble { + background: color-mix(in srgb, var(--udt-surface0) 88%, transparent); + border: 1px solid var(--udt-surface1); border-bottom-left-radius: 4px; +} +.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 .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; } 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); diff --git a/tests/test_server.py b/tests/test_server.py index 0881849..4755a03 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -114,6 +114,8 @@ class Server(unittest.TestCase): self.assertEqual(self.req("GET", "/api/bg", {"Host": "127.0.0.1:8765"})[0], 401) with mock.patch.dict(srv.CFG, {"assistant": {"background": ""}}): self.assertEqual(self.req("GET", "/api/bg", HOST)[0], 404) + with mock.patch.dict(srv.CFG, {"assistant": {"user_avatar": str(f)}}): + self.assertEqual(self.req("GET", "/api/avatar", HOST), (200, b"\xff\xd8jpeg")) def test_module_images(self): self.assertEqual(self.req("GET", "/api/demo/image/k1", HOST), (200, b"img")) |
