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.js33
1 files changed, 24 insertions, 9 deletions
diff --git a/static/modules/assistant.js b/static/modules/assistant.js
index 9f38c0a..dcb6f27 100644
--- a/static/modules/assistant.js
+++ b/static/modules/assistant.js
@@ -94,7 +94,6 @@ function turnNode(t, live) {
}
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;
@@ -107,7 +106,7 @@ function renderLog(log, st, el) {
live.classList.add("as-live");
log.append(live);
}
- if (bottom) log.scrollTop = log.scrollHeight;
+ if (el._stick) log.scrollTop = log.scrollHeight;
}
function renderStrip(strip, st, el) {
@@ -156,7 +155,7 @@ function renderOp(box, op) {
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}`;
+ : op.ok ? `Saved: ${[].concat(op.out ?? []).map(name).join(", ")}` : `${op.label}: ${op.error}`;
}
function renderModels(panel, st, el) {
@@ -178,15 +177,26 @@ function renderModels(panel, st, el) {
async function toggleMic(el) {
if (el._rec) { el._rec.stop(); return; }
- let stream;
+ if (el._recStarting) return;
+ el._recStarting = true;
+ let stream, rec;
try {
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
+ el._micDenied = false;
} catch (e) {
el._micDenied = true;
note(el, `microphone: ${e.message}`);
return;
+ } finally {
+ el._recStarting = false;
+ }
+ try {
+ rec = new MediaRecorder(stream);
+ } catch (e) {
+ stream.getTracks().forEach((t) => t.stop());
+ note(el, `microphone: ${e.message}`);
+ return;
}
- const rec = new MediaRecorder(stream);
const chunks = [];
rec.ondataavailable = (e) => chunks.push(e.data);
rec.onstop = async () => {
@@ -242,7 +252,12 @@ export default {
panel.hidden = !panel.hidden;
if (!panel.hidden) act(el, "models");
};
- el.querySelector(".as-log").onclick = () => { panel.hidden = true; };
+ const log = el.querySelector(".as-log");
+ el._stick = true;
+ 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;
+ });
const bar = el.querySelector(".as-bar");
const input = bar.querySelector("textarea");
const mic = iconBtn(G.mic, "Ask by voice", "mic");
@@ -253,10 +268,10 @@ export default {
const text = input.value.trim();
if (!text) return;
input.value = "";
- act(el, "ask", { text });
+ 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.preventDefault(); send.onclick(); }
+ if (e.key === "Enter" && !e.shiftKey && !e.isComposing) { e.preventDefault(); if (!el._busy) send.onclick(); }
});
bar.append(mic, send);
},
@@ -298,7 +313,7 @@ export default {
send.title = send.getAttribute("aria-label");
const mic = el.querySelector(".mic");
const deaf = !st.hears || el._micDenied;
- mic.disabled = deaf && !el._rec;
+ mic.disabled = !st.hears && !el._rec;
mic.textContent = deaf ? G.micOff : G.mic;
mic.classList.toggle("on", !!el._rec);
mic.title = el._micDenied ? "Microphone not allowed"