aboutsummaryrefslogtreecommitdiffstats
path: root/static
diff options
context:
space:
mode:
Diffstat (limited to 'static')
-rw-r--r--static/deck.css62
-rw-r--r--static/modules/assistant.js309
2 files changed, 371 insertions, 0 deletions
diff --git a/static/deck.css b/static/deck.css
index 0b9a0dd..5d0e66a 100644
--- a/static/deck.css
+++ b/static/deck.css
@@ -605,3 +605,65 @@ select {
.estop > span { position: relative; }
.estop-label { font-size: 1.071rem; text-transform: uppercase; letter-spacing: .04em; }
.estop-hint { font-size: 0.714rem; font-weight: 400; opacity: .7; }
+
+/* Assistant tab. Icons are the Nerd Font glyphs the canvas uses, served by
+ deck-it from the PC; every control is at least 48px for a finger. */
+@font-face { font-family: "deckit-nerd"; src: url("/fonts/nerd.ttf") format("truetype"); font-display: block; }
+.nf { font-family: "deckit-nerd"; font-style: normal; }
+
+.assistant { display: flex; flex-direction: column; height: calc(100dvh - 120px); }
+.assistant .as-main { display: flex; flex-direction: column; gap: 10px; flex: 1; min-height: 0; position: relative; }
+.assistant .ib {
+ min-width: 48px; min-height: 48px; border-radius: 12px; font-size: 1.4rem;
+ background: var(--udt-surface0); border: 1px solid var(--udt-surface1); color: var(--udt-text); cursor: pointer;
+}
+.assistant .ib:active { background: var(--udt-surface1); }
+.assistant .ib:disabled { opacity: .4; }
+.assistant .as-head { display: flex; align-items: center; gap: 10px; }
+.assistant .as-model {
+ min-height: 48px; padding: 0 14px; border-radius: 12px; font: inherit; display: flex; gap: 8px; align-items: center;
+ 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-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;
+}
+.assistant .as-mrow {
+ display: flex; align-items: center; gap: 10px; width: 100%; min-height: 48px; padding: 0 10px; font: inherit;
+ background: none; border: 1px solid transparent; border-radius: 8px; color: var(--udt-text); text-align: left;
+}
+.assistant .as-mrow.on { border-color: var(--udt-accent); }
+.assistant .as-mrow span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
+.assistant .as-mrow small { color: var(--udt-overlay0); }
+.assistant .as-row { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; }
+.assistant .as-tile {
+ flex: none; width: 84px; min-height: 72px; display: flex; flex-direction: column; align-items: center;
+ justify-content: center; gap: 4px; border-radius: 12px; font: inherit; cursor: pointer;
+ background: var(--udt-surface0); border: 1px solid var(--udt-surface1); color: var(--udt-text);
+}
+.assistant .as-row.op .as-tile { background: var(--udt-surface1); }
+.assistant .as-tile i { font-size: 1.6rem; }
+.assistant .as-tile span { font-size: 0.714rem; color: var(--udt-subtext0); max-width: 76px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
+.assistant .as-picking { display: flex; align-items: center; gap: 12px; min-height: 72px; color: var(--udt-yellow); }
+.assistant .as-picking span { flex: 1; }
+.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-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; }
+.assistant .md pre .copy { position: absolute; top: 4px; right: 4px; }
+.assistant .md code { font-family: "Inconsolata Nerd Font Mono", ui-monospace, monospace; }
+.assistant .md table { border-collapse: collapse; }
+.assistant .md th, .assistant .md td { border: 1px solid var(--udt-surface1); padding: 4px 8px; }
+.assistant .md a { color: var(--udt-accent); }
+.assistant .as-bar { display: flex; gap: 8px; align-items: flex-end; }
+.assistant .as-bar textarea {
+ flex: 1; min-height: 48px; max-height: 30vh; resize: none; font: inherit; padding: 12px; border-radius: 12px;
+ background: var(--udt-surface0); border: 1px solid var(--udt-surface1); color: var(--udt-text); field-sizing: content;
+}
+.assistant .mic.on { background: var(--udt-red); border-color: var(--udt-red); color: var(--udt-crust); }
+.assistant .mic.on::after { content: attr(data-secs); font-family: inherit; font-size: 0.714rem; margin-left: 4px; }
diff --git a/static/modules/assistant.js b/static/modules/assistant.js
new file mode 100644
index 0000000..9f38c0a
--- /dev/null
+++ b/static/modules/assistant.js
@@ -0,0 +1,309 @@
+// deck-it - assistant panel
+// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as
+// published by the Free Software Foundation.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+// desktop-assistant on the tablet: chat with formatted streamed replies,
+// voice through the tablet's mic, model choice, and the canvas's Ask
+// presets and Do ops on a file picked on the PC. Every control is an icon
+// (the Nerd Font the server serves), sized for a finger.
+
+import { offline } from "../offline.js";
+import { render } from "../md.js";
+import { toWav } from "../wav.js";
+
+// Font Awesome codepoints, present in Inconsolata Nerd Font.
+const G = { send: "", stop: "", mic: "", micOff: "", plus: "",
+ cloud: "", close: "", copy: "", fallback: "" };
+const MAX_REC = 59;
+const STATUS = { thinking: "thinking…", transcribing: "transcribing…" };
+
+async function post(action, body = {}) {
+ const r = await fetch(`/api/assistant/${action}`, {
+ method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body),
+ });
+ if (!r.ok) throw new Error((await r.text()) || r.statusText);
+}
+
+function iconBtn(glyph, label, cls = "") {
+ const b = document.createElement("button");
+ b.className = `ib nf ${cls}`;
+ b.textContent = glyph;
+ b.setAttribute("aria-label", label);
+ b.title = label;
+ return b;
+}
+
+function note(el, text) {
+ el._note = text;
+ clearTimeout(el._noteT);
+ el._noteT = setTimeout(() => { el._note = ""; }, 6000);
+}
+
+function act(el, action, body) {
+ post(action, body).catch((e) => note(el, e.message));
+}
+
+function addCopy(box) {
+ for (const pre of box.querySelectorAll("pre")) {
+ const b = iconBtn(G.copy, "Copy", "copy");
+ // The tablet's clipboard; the button's own glyph is outside <code>.
+ b.onclick = () => navigator.clipboard?.writeText(pre.querySelector("code")?.innerText ?? "");
+ pre.append(b);
+ }
+}
+
+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);
+ }
+ if (t.tools?.length) {
+ const tools = document.createElement("div");
+ tools.className = "as-tools";
+ tools.textContent = t.tools.join(" · ");
+ d.append(tools);
+ }
+ const a = document.createElement("div");
+ a.className = "as-a md";
+ a.innerHTML = render(t.reply);
+ if (!live) addCopy(a);
+ d.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);
+ }
+ return d;
+}
+
+function renderLog(log, st, el) {
+ const bottom = log.scrollHeight - log.scrollTop - log.clientHeight < 40;
+ 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);
+ }
+ if (bottom) log.scrollTop = log.scrollHeight;
+}
+
+function renderStrip(strip, st, el) {
+ if (st.picking) {
+ const key = `picking:${st.picking}`;
+ if (strip._key === key) return;
+ strip._key = key;
+ strip.innerHTML = `<div class="as-picking"><span></span></div>`;
+ strip.querySelector("span").textContent = `${st.picking}: choose the file on the PC`;
+ const cancel = iconBtn(G.close, "Cancel");
+ cancel.onclick = () => act(el, "cancel");
+ strip.firstElementChild.append(cancel);
+ return;
+ }
+ const cat = st.catalog;
+ const key = JSON.stringify([cat?.presets, cat?.ops, st.catalogError]);
+ if (strip._key === key) return;
+ strip._key = key;
+ strip.textContent = "";
+ if (!cat) {
+ strip.innerHTML = `<div class="as-note"></div>`;
+ strip.firstElementChild.textContent = st.catalogError || "Loading presets…";
+ return;
+ }
+ for (const [kind, items] of [["preset", cat.presets ?? []], ["op", cat.ops ?? []]]) {
+ const row = document.createElement("div");
+ row.className = `as-row ${kind}`;
+ for (const item of items) {
+ const b = document.createElement("button");
+ b.className = "as-tile";
+ b.title = item.prompt ?? item.label;
+ b.setAttribute("aria-label", item.label);
+ b.innerHTML = `<i class="nf"></i><span></span>`;
+ b.querySelector("i").textContent = item.icon || G.fallback;
+ b.querySelector("span").textContent = item.label;
+ b.onclick = () => act(el, "pick", { [kind]: item.label });
+ row.append(b);
+ }
+ strip.append(row);
+ }
+}
+
+function renderOp(box, op) {
+ box.hidden = !op;
+ if (!op) return;
+ const name = (p) => p.slice(p.lastIndexOf("/") + 1);
+ box.className = `as-op ${op.running ? "" : op.ok ? "good" : "bad"}`;
+ box.textContent = op.running ? `${op.label}…`
+ : op.ok ? `Saved: ${op.out.map(name).join(", ")}` : `${op.label}: ${op.error}`;
+}
+
+function renderModels(panel, st, el) {
+ const key = JSON.stringify([st.models, st.wanted]);
+ if (panel._key === key) return;
+ panel._key = key;
+ panel.textContent = "";
+ for (const m of st.models) {
+ const b = document.createElement("button");
+ b.className = `as-mrow${m.id === st.wanted ? " on" : ""}`;
+ b.innerHTML = `<i class="nf"></i><span></span><small></small>`;
+ b.querySelector("i").textContent = m.cloud ? G.cloud : "";
+ b.querySelector("span").textContent = m.id;
+ b.querySelector("small").textContent = m.cloud ? m.provider : m.status;
+ b.onclick = () => { panel.hidden = true; act(el, "model", { id: m.id }); };
+ panel.append(b);
+ }
+}
+
+async function toggleMic(el) {
+ if (el._rec) { el._rec.stop(); return; }
+ let stream;
+ try {
+ stream = await navigator.mediaDevices.getUserMedia({ audio: true });
+ } catch (e) {
+ el._micDenied = true;
+ note(el, `microphone: ${e.message}`);
+ return;
+ }
+ const rec = new MediaRecorder(stream);
+ const chunks = [];
+ rec.ondataavailable = (e) => chunks.push(e.data);
+ rec.onstop = async () => {
+ stream.getTracks().forEach((t) => t.stop());
+ clearTimeout(el._recCap);
+ const secs = (Date.now() - el._recAt) / 1000;
+ el._rec = null;
+ if (secs < 0.5) return;
+ try {
+ const wav = await toWav(new Blob(chunks, { type: rec.mimeType }));
+ const r = await fetch("/api/assistant/voice", {
+ method: "POST", headers: { "Content-Type": "audio/wav" }, body: wav,
+ });
+ if (!r.ok) throw new Error((await r.text()) || r.statusText);
+ } catch (e) {
+ note(el, `voice: ${e.message}`);
+ }
+ };
+ el._rec = rec;
+ el._recAt = Date.now();
+ rec.start();
+ el._recCap = setTimeout(() => rec.state === "recording" && rec.stop(), MAX_REC * 1000);
+}
+
+export default {
+ title: "Assistant",
+ tab: "Assistant",
+ interval: 0.5,
+
+ mount(el) {
+ el.innerHTML = `
+ <div class="assistant">
+ <div class="as-off"></div>
+ <div class="as-main">
+ <div class="as-head">
+ <button class="as-model"><i class="nf"></i><span></span></button>
+ <span class="as-status"></span>
+ </div>
+ <div class="as-models" hidden></div>
+ <div class="as-strip"></div>
+ <div class="as-op" hidden></div>
+ <div class="as-log"></div>
+ <div class="as-notice"></div>
+ <div class="as-bar"><textarea rows="1" aria-label="Question"></textarea></div>
+ </div>
+ </div>`;
+ const head = el.querySelector(".as-head");
+ const fresh = iconBtn(G.plus, "New chat");
+ fresh.onclick = () => act(el, "new");
+ head.append(fresh);
+ const panel = el.querySelector(".as-models");
+ el.querySelector(".as-model").onclick = () => {
+ panel.hidden = !panel.hidden;
+ if (!panel.hidden) act(el, "models");
+ };
+ el.querySelector(".as-log").onclick = () => { panel.hidden = true; };
+ const bar = el.querySelector(".as-bar");
+ const input = bar.querySelector("textarea");
+ const mic = iconBtn(G.mic, "Ask by voice", "mic");
+ mic.onclick = () => toggleMic(el);
+ const send = iconBtn(G.send, "Send", "send");
+ send.onclick = () => {
+ if (el._busy) return act(el, "stop");
+ const text = input.value.trim();
+ if (!text) return;
+ input.value = "";
+ act(el, "ask", { text });
+ };
+ input.addEventListener("keydown", (e) => {
+ if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); send.onclick(); }
+ });
+ bar.append(mic, send);
+ },
+
+ update(st, el) {
+ const main = el.querySelector(".as-main");
+ const off = el.querySelector(".as-off");
+ main.hidden = !st.connected;
+ if (!st.connected) {
+ offline(off, {
+ icon: "/icons/assistant.svg", // hidden by offline.js when the theme has none
+ title: "Assistant not running",
+ detail: "Start desktop-assistant and the chat appears here.",
+ reason: st.error,
+ });
+ return;
+ }
+ off.textContent = "";
+ off._offline = null;
+
+ el._busy = !!st.pending || st.state !== "idle";
+ const model = el.querySelector(".as-model");
+ 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);
+ renderStrip(el.querySelector(".as-strip"), st, el);
+ renderOp(el.querySelector(".as-op"), st.op);
+ renderLog(el.querySelector(".as-log"), st, el);
+ el.querySelector(".as-notice").textContent = el._note || st.notice || "";
+
+ const send = el.querySelector(".send");
+ send.textContent = el._busy ? G.stop : G.send;
+ send.setAttribute("aria-label", el._busy ? "Stop" : "Send");
+ send.title = send.getAttribute("aria-label");
+ const mic = el.querySelector(".mic");
+ const deaf = !st.hears || el._micDenied;
+ mic.disabled = deaf && !el._rec;
+ mic.textContent = deaf ? G.micOff : G.mic;
+ mic.classList.toggle("on", !!el._rec);
+ mic.title = el._micDenied ? "Microphone not allowed"
+ : !st.hears ? `${st.wanted || st.model} takes no audio`
+ : el._rec ? `Recording ${Math.floor((Date.now() - el._recAt) / 1000)}s, tap to send` : "Ask by voice";
+ mic.dataset.secs = el._rec ? `${Math.floor((Date.now() - el._recAt) / 1000)}` : "";
+ },
+};