diff options
Diffstat (limited to 'static')
| -rw-r--r-- | static/deck.css | 16 | ||||
| -rw-r--r-- | static/modules/assistant.js | 87 | ||||
| -rw-r--r-- | static/wav.js | 8 |
3 files changed, 67 insertions, 44 deletions
diff --git a/static/deck.css b/static/deck.css index 60a9b56..8d17a66 100644 --- a/static/deck.css +++ b/static/deck.css @@ -638,7 +638,14 @@ select { .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 .fresh { margin-left: auto; } +.assistant .as-strip { + position: absolute; top: 58px; left: 0; right: 0; z-index: 2; max-height: 75%; overflow-y: auto; + display: flex; flex-direction: column; gap: 10px; + background: var(--udt-mantle); border: 1px solid var(--udt-surface1); border-radius: 12px; padding: 10px; +} +.assistant .as-strip[hidden], .assistant .as-picking[hidden] { display: none; } +.assistant .as-row { display: flex; flex-wrap: wrap; gap: 8px; } .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; @@ -661,11 +668,8 @@ select { .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 .as-bar { display: flex; gap: 12px; align-items: center; } +.assistant .as-bar .mic { min-width: 72px; min-height: 72px; font-size: 1.9rem; border-radius: 36px; } .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; } .assistant .as-op.good { color: var(--udt-green); } diff --git a/static/modules/assistant.js b/static/modules/assistant.js index dcb6f27..0159936 100644 --- a/static/modules/assistant.js +++ b/static/modules/assistant.js @@ -21,12 +21,15 @@ import { offline } from "../offline.js"; import { render } from "../md.js"; -import { toWav } from "../wav.js"; +import { peak, toWav } from "../wav.js"; // Font Awesome codepoints, present in Inconsolata Nerd Font. -const G = { send: "", stop: "", mic: "", micOff: "", plus: "", +const G = { tools: "", stop: "", mic: "", micOff: "", plus: "", cloud: "", close: "", copy: "", fallback: "" }; const MAX_REC = 59; +// Below this peak a clip is silence (room noise measured ~260, speech +// 15000+); the model would invent a question from it, so it is not sent. +const SILENT = 1500; const STATUS = { thinking: "thinking…", transcribing: "transcribing…" }; async function post(action, body = {}) { @@ -109,18 +112,20 @@ function renderLog(log, st, el) { if (el._stick) log.scrollTop = log.scrollHeight; } +function renderPicking(box, st, el) { + box.hidden = !st.picking; + if (!st.picking || box._key === st.picking) return; + box._key = st.picking; + box.innerHTML = `<span></span>`; + box.firstElementChild.textContent = `${st.picking}: choose the file on the PC`; + const cancel = iconBtn(G.close, "Cancel"); + cancel.onclick = () => act(el, "cancel"); + box.append(cancel); +} + +// The presets and ops, in an overlay opened from the header, so the +// transcript keeps the screen. 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; @@ -142,7 +147,7 @@ function renderStrip(strip, st, el) { 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 }); + b.onclick = () => { strip.hidden = true; act(el, "pick", { [kind]: item.label }); }; row.append(b); } strip.append(row); @@ -175,10 +180,17 @@ function renderModels(panel, st, el) { } } +// The mic's look follows the recorder at once, not at the next poll: a tap +// with no feedback invites a second one, which made a silent clip. +function micLook(el) { + el.querySelector(".mic").classList.toggle("on", !!(el._rec || el._recStarting)); +} + async function toggleMic(el) { if (el._rec) { el._rec.stop(); return; } if (el._recStarting) return; el._recStarting = true; + micLook(el); let stream, rec; try { stream = await navigator.mediaDevices.getUserMedia({ audio: true }); @@ -189,6 +201,7 @@ async function toggleMic(el) { return; } finally { el._recStarting = false; + micLook(el); } try { rec = new MediaRecorder(stream); @@ -204,9 +217,11 @@ async function toggleMic(el) { clearTimeout(el._recCap); const secs = (Date.now() - el._recAt) / 1000; el._rec = null; + micLook(el); if (secs < 0.5) return; try { const wav = await toWav(new Blob(chunks, { type: rec.mimeType })); + if (peak(wav) < SILENT) return note(el, "nothing heard"); const r = await fetch("/api/assistant/voice", { method: "POST", headers: { "Content-Type": "audio/wav" }, body: wav, }); @@ -218,6 +233,7 @@ async function toggleMic(el) { el._rec = rec; el._recAt = Date.now(); rec.start(); + micLook(el); el._recCap = setTimeout(() => rec.state === "recording" && rec.stop(), MAX_REC * 1000); } @@ -233,23 +249,27 @@ export default { <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-strip" hidden></div> + <div class="as-picking" hidden></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 class="as-bar"><span class="as-status"></span></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"); + const strip = el.querySelector(".as-strip"); + const tools = iconBtn(G.tools, "Presets and file operations", "tools"); + tools.onclick = () => { strip.hidden = !strip.hidden; panel.hidden = true; }; + const fresh = iconBtn(G.plus, "New chat", "fresh"); + fresh.onclick = () => act(el, "new"); + head.append(tools, fresh); el.querySelector(".as-model").onclick = () => { panel.hidden = !panel.hidden; + strip.hidden = true; if (!panel.hidden) act(el, "models"); }; const log = el.querySelector(".as-log"); @@ -257,23 +277,16 @@ export default { log.addEventListener("scroll", () => { el._stick = log.scrollHeight - log.scrollTop - log.clientHeight < 40; }); el.addEventListener("pointerdown", (e) => { if (!e.target.closest(".as-models, .as-model")) panel.hidden = true; + if (!e.target.closest(".as-strip, .tools")) strip.hidden = true; }); + // No keyboard: the PC is at hand for typing. Voice, and Stop while a + // reply streams. 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 = ""; - post("ask", { text }).catch((e) => { if (!input.value) input.value = text; note(el, e.message); }); - }; - input.addEventListener("keydown", (e) => { - if (e.key === "Enter" && !e.shiftKey && !e.isComposing) { e.preventDefault(); if (!el._busy) send.onclick(); } - }); - bar.append(mic, send); + const stop = iconBtn(G.stop, "Stop", "stop"); + stop.onclick = () => act(el, "stop"); + bar.append(mic, stop); }, update(st, el) { @@ -303,19 +316,17 @@ export default { const panel = el.querySelector(".as-models"); if (!panel.hidden) renderModels(panel, st, el); renderStrip(el.querySelector(".as-strip"), st, el); + renderPicking(el.querySelector(".as-picking"), 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"); + el.querySelector(".stop").hidden = !el._busy; const mic = el.querySelector(".mic"); const deaf = !st.hears || el._micDenied; mic.disabled = !st.hears && !el._rec; mic.textContent = deaf ? G.micOff : G.mic; - mic.classList.toggle("on", !!el._rec); + micLook(el); 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"; diff --git a/static/wav.js b/static/wav.js index ada6302..b83fae2 100644 --- a/static/wav.js +++ b/static/wav.js @@ -34,6 +34,14 @@ export function encodeWav(samples, rate) { return buf; } +// The loudest sample of a 16-bit WAV from encodeWav, to tell silence. +export function peak(wav) { + const v = new DataView(wav); + let p = 0; + for (let i = 44; i + 1 < wav.byteLength; i += 2) p = Math.max(p, Math.abs(v.getInt16(i, true))); + return p; +} + export async function toWav(blob, rate = 16000) { const ctx = new AudioContext(); try { |
