1409 lines
45 KiB
JavaScript
1409 lines
45 KiB
JavaScript
// =========================================================
|
|
// app.js — Arisu AI
|
|
// =========================================================
|
|
|
|
const I18N = {
|
|
es: {
|
|
connecting: "conectando...",
|
|
connected: "conectada",
|
|
disconnected: "desconectada",
|
|
settings: "Ajustes",
|
|
clearChat: "Limpiar chat",
|
|
uiLang: "Idioma UI",
|
|
sttLang: "Idioma voz",
|
|
ttsEnabled: "Reproducir voz",
|
|
model: "Modelo",
|
|
camera: "Cámara",
|
|
microphone: "Micrófono",
|
|
speak: "Hablar",
|
|
stop: "Detener",
|
|
send: "Enviar",
|
|
audio: "Audio",
|
|
recording: "Grabando...",
|
|
transcribing: "Transcribiendo...",
|
|
noAudio: "(sin audio)",
|
|
noVoice: "(sin voz)",
|
|
placeholder: "Escríbele algo a Arisu...",
|
|
welcomeTitle: "¡Hola! Soy Arisu",
|
|
welcomeBody: 'Di <span class="kbd-badge">«Arisu»</span> y habla,<br/>pulsa <span class="kbd-badge">🎙️ Hablar</span>, o escribe.<br/>Te sigo con la mirada (◕‿◕)♡',
|
|
wakeListen: 'Di «Arisu»…',
|
|
wakeListening: 'Escuchando…',
|
|
wakeEnabled: 'Activar con «Arisu»',
|
|
wakeDetected: '¡Te escucho!',
|
|
wakeChecking: 'Detectando…',
|
|
you: "Tú",
|
|
arisu: "Arisu ✨",
|
|
loadingModel: "Cargando modelo Live2D...",
|
|
loadingNamed: "Cargando {name}...",
|
|
loadError: "Error al cargar modelo. Recarga la página.",
|
|
noConnection: "Sin conexión.",
|
|
micNotReady: "Micrófono no listo.",
|
|
micUnavailable: "Micrófono no disponible: {msg}",
|
|
ttsError: "Voz no disponible: {msg}",
|
|
audioError: "Audio: {msg}",
|
|
busy: "Espera, Arisu está respondiendo...",
|
|
cancelled: "Respuesta cancelada.",
|
|
cameraActive: "Cámara activa ✓",
|
|
noCamera: "Sin cámara",
|
|
noFace: "Sin rostro",
|
|
faceDetected: "Rostro detectado ✓",
|
|
starting: "iniciando...",
|
|
noDevice: "Sin {label}",
|
|
deviceLabel: "{label} {n}",
|
|
terminalEnabled: "Activar modo terminal",
|
|
terminalSection: "Modo terminal",
|
|
terminalBadge: "Terminal",
|
|
terminalActiveTitle: "Modo terminal activo",
|
|
generalSection: "General",
|
|
workspace: "Directorio base",
|
|
workspacePlaceholder: "/Users/tu/proyectos",
|
|
workspaceOff: "Terminal desactivado",
|
|
workspaceActive: "Workspace: {path}",
|
|
workspaceError: "Workspace inválido: {msg}",
|
|
toolRunning: "⚙ {tool}",
|
|
toolDone: "✓ {tool}: {preview}",
|
|
toolWebSearch: "Buscando en la web",
|
|
toolListDir: "Listando carpeta",
|
|
toolReadFile: "Leyendo archivo",
|
|
toolWriteFile: "Escribiendo archivo",
|
|
toolMkdir: "Creando carpeta",
|
|
toolRunCmd: "Ejecutando comando",
|
|
toolWeather: "Consultando clima",
|
|
weatherHumidity: "Humedad",
|
|
weatherWind: "Viento",
|
|
weatherToday: "Hoy",
|
|
weatherUnitWind: "km/h",
|
|
welcomeBodyTerminal: 'Pulsa <span class="kbd-badge">🎙️ Hablar</span> o escribe.<br/>Con <strong>modo terminal</strong> puedo crear archivos y ejecutar comandos en tu carpeta base ✨',
|
|
},
|
|
en: {
|
|
connecting: "connecting...",
|
|
connected: "connected",
|
|
disconnected: "disconnected",
|
|
settings: "Settings",
|
|
clearChat: "Clear chat",
|
|
uiLang: "UI language",
|
|
sttLang: "Voice language",
|
|
ttsEnabled: "Play voice",
|
|
model: "Model",
|
|
camera: "Camera",
|
|
microphone: "Microphone",
|
|
speak: "Speak",
|
|
stop: "Stop",
|
|
send: "Send",
|
|
audio: "Audio",
|
|
recording: "Recording...",
|
|
transcribing: "Transcribing...",
|
|
noAudio: "(no audio)",
|
|
noVoice: "(no voice)",
|
|
placeholder: "Write something to Arisu...",
|
|
welcomeTitle: "Hi! I'm Arisu",
|
|
welcomeBody: 'Say <span class="kbd-badge">«Arisu»</span> and speak,<br/>press <span class="kbd-badge">🎙️ Speak</span>, or type.<br/>I follow you with my eyes (◕‿◕)♡',
|
|
wakeListen: 'Say «Arisu»…',
|
|
wakeListening: 'Listening…',
|
|
wakeEnabled: 'Activate on «Arisu»',
|
|
wakeDetected: 'I hear you!',
|
|
wakeChecking: 'Detecting…',
|
|
you: "You",
|
|
arisu: "Arisu ✨",
|
|
loadingModel: "Loading Live2D model...",
|
|
loadingNamed: "Loading {name}...",
|
|
loadError: "Failed to load model. Reload the page.",
|
|
noConnection: "No connection.",
|
|
micNotReady: "Microphone not ready.",
|
|
micUnavailable: "Microphone unavailable: {msg}",
|
|
ttsError: "Voice unavailable: {msg}",
|
|
audioError: "Audio: {msg}",
|
|
busy: "Wait, Arisu is responding...",
|
|
cancelled: "Response cancelled.",
|
|
cameraActive: "Camera active ✓",
|
|
noCamera: "No camera",
|
|
noFace: "No face",
|
|
faceDetected: "Face detected ✓",
|
|
starting: "starting...",
|
|
noDevice: "No {label}",
|
|
deviceLabel: "{label} {n}",
|
|
terminalEnabled: "Enable terminal mode",
|
|
terminalSection: "Terminal mode",
|
|
terminalBadge: "Terminal",
|
|
terminalActiveTitle: "Terminal mode active",
|
|
generalSection: "General",
|
|
workspace: "Base directory",
|
|
workspacePlaceholder: "/Users/you/projects",
|
|
workspaceOff: "Terminal disabled",
|
|
workspaceActive: "Workspace: {path}",
|
|
workspaceError: "Invalid workspace: {msg}",
|
|
toolRunning: "⚙ {tool}",
|
|
toolDone: "✓ {tool}: {preview}",
|
|
toolWebSearch: "Searching the web",
|
|
toolListDir: "Listing folder",
|
|
toolReadFile: "Reading file",
|
|
toolWriteFile: "Writing file",
|
|
toolMkdir: "Creating folder",
|
|
toolRunCmd: "Running command",
|
|
toolWeather: "Fetching weather",
|
|
weatherHumidity: "Humidity",
|
|
weatherWind: "Wind",
|
|
weatherToday: "Today",
|
|
weatherUnitWind: "km/h",
|
|
welcomeBodyTerminal: 'Press <span class="kbd-badge">🎙️ Speak</span> or type.<br/>With <strong>terminal mode</strong> I can create files and run commands in your base folder ✨',
|
|
},
|
|
};
|
|
|
|
// ── Wake word + VAD (Safari-compatible: audio + Whisper) ─────
|
|
const WAKE_WORD_RE = /\b(arisu|ariso|alice|alisa)\b|アリス/i;
|
|
const VAD_SPEECH_RMS = 0.018;
|
|
const VAD_SILENCE_MS = 1400;
|
|
const VAD_MIN_SPEECH_MS = 450;
|
|
const VAD_MAX_RECORD_MS = 30000;
|
|
const WAKE_SILENCE_MS = 1700;
|
|
const WAKE_MAX_MS = 9000;
|
|
const WAKE_DEBOUNCE_MS = 100;
|
|
const WAKE_PREROLL_MS = 350;
|
|
const WAKE_POST_BUFFER_MS = 6000;
|
|
|
|
const state = {
|
|
ws: null,
|
|
avatar: null,
|
|
uiLang: localStorage.getItem("arisu_ui_lang") || "es",
|
|
ttsEnabled: localStorage.getItem("arisu_tts") !== "false",
|
|
wakeEnabled: localStorage.getItem("arisu_wake") !== "false",
|
|
terminalEnabled: localStorage.getItem("arisu_terminal") === "true",
|
|
workspacePath: localStorage.getItem("arisu_workspace") || "",
|
|
|
|
audioCtx: null,
|
|
audioSource: null,
|
|
analyser: null,
|
|
audioData: null,
|
|
audioStream: null,
|
|
silentGain: null,
|
|
continuousProcessor: null,
|
|
listenActive: false,
|
|
isRecording: false,
|
|
recordingAutoStop: false,
|
|
pcmChunks: [],
|
|
recordingSampleRate: 16000,
|
|
|
|
wakePaused: false,
|
|
wakeChecking: false,
|
|
wakeCaptureActive: false,
|
|
wakeChunks: [],
|
|
wakeHadSpeech: false,
|
|
wakeSilenceMs: 0,
|
|
wakeTotalMs: 0,
|
|
wakePendingMs: 0,
|
|
wakePreRoll: [],
|
|
wakePostBuffer: [],
|
|
|
|
vadHadSpeech: false,
|
|
vadSilenceMs: 0,
|
|
vadSpeechMs: 0,
|
|
vadTotalMs: 0,
|
|
|
|
camStream: null,
|
|
camera: null,
|
|
faceMesh: null,
|
|
|
|
selectedCamId: null,
|
|
selectedMicId: null,
|
|
selectedLang: "es-ES",
|
|
selectedModel: "mao",
|
|
|
|
responding: false,
|
|
ttsSource: null,
|
|
ttsAnalyser: null,
|
|
geoLat: null,
|
|
geoLon: null,
|
|
};
|
|
|
|
const WEATHER_ICONS = {
|
|
clear: "bi-sun-fill",
|
|
"mostly-clear": "bi-sun",
|
|
"partly-cloudy": "bi-cloud-sun-fill",
|
|
cloudy: "bi-cloud-fill",
|
|
fog: "bi-cloud-fog2-fill",
|
|
drizzle: "bi-cloud-drizzle-fill",
|
|
rain: "bi-cloud-rain-fill",
|
|
"rain-heavy": "bi-cloud-rain-heavy-fill",
|
|
snow: "bi-snow",
|
|
showers: "bi-cloud-rain-fill",
|
|
"showers-heavy": "bi-cloud-rain-heavy-fill",
|
|
thunder: "bi-cloud-lightning-rain-fill",
|
|
};
|
|
|
|
// ── DOM ──────────────────────────────────────────────────────
|
|
const $ = id => document.getElementById(id);
|
|
const avatarCanvas = $("avatarCanvas");
|
|
const audioCanvas = $("audioCanvas");
|
|
const webcamVideo = $("webcamVideo");
|
|
const chatMessages = $("chatMessages");
|
|
const transcriptEl = $("transcript");
|
|
const statusDot = $("statusDot");
|
|
const statusTxt = $("statusTxt");
|
|
const micBtn = $("micBtn");
|
|
const micIcon = $("micIcon");
|
|
const micLabel = $("micLabel");
|
|
const stopBtn = $("stopBtn");
|
|
const sendBtn = $("sendBtn");
|
|
const textInput = $("textInput");
|
|
const faceStatus = $("faceStatus");
|
|
const settingsBtn = $("settingsBtn");
|
|
const clearChatBtn = $("clearChatBtn");
|
|
const settingsPanel = $("settingsPanel");
|
|
const camSelect = $("camSelect");
|
|
const micSelect = $("micSelect");
|
|
const langSelect = $("langSelect");
|
|
const uiLangSelect = $("uiLangSelect");
|
|
const ttsToggle = $("ttsToggle");
|
|
const wakeToggle = $("wakeToggle");
|
|
const terminalToggle = $("terminalToggle");
|
|
const workspaceInput = $("workspaceInput");
|
|
const workspaceStatus = $("workspaceStatus");
|
|
const terminalBadge = $("terminalBadge");
|
|
const modelSelect = $("modelSelect");
|
|
const loadingOverlay = $("loadingOverlay");
|
|
const loadingTxt = $("loadingTxt");
|
|
const micDot = $("micDot");
|
|
const audioLabel = $("audioLabel");
|
|
|
|
function t(key, vars = {}) {
|
|
let s = I18N[state.uiLang]?.[key] ?? I18N.es[key] ?? key;
|
|
for (const [k, v] of Object.entries(vars)) s = s.replace(`{${k}}`, v);
|
|
return s;
|
|
}
|
|
|
|
function applyI18n() {
|
|
document.documentElement.lang = state.uiLang;
|
|
|
|
// data-* attributes (legacy)
|
|
document.querySelectorAll("[data-i18n]").forEach(el => {
|
|
el.textContent = t(el.dataset.i18n);
|
|
});
|
|
document.querySelectorAll("[data-i18n-html]").forEach(el => {
|
|
el.innerHTML = t(el.dataset.i18nHtml);
|
|
});
|
|
document.querySelectorAll("[data-i18n-title]").forEach(el => {
|
|
el.title = t(el.dataset.i18nTitle);
|
|
});
|
|
|
|
// Settings panel — IDs explícitos (evita claves literales en pantalla)
|
|
const map = {
|
|
lblTerminalSection: "terminalSection",
|
|
lblGeneralSection: "generalSection",
|
|
lblTerminalEnabled: "terminalEnabled",
|
|
lblWorkspace: "workspace",
|
|
lblModel: "model",
|
|
lblCamera: "camera",
|
|
lblMic: "microphone",
|
|
lblUiLang: "uiLang",
|
|
lblSttLang: "sttLang",
|
|
lblTtsEnabled: "ttsEnabled",
|
|
lblWakeEnabled: "wakeEnabled",
|
|
terminalBadgeTxt: "terminalBadge",
|
|
};
|
|
for (const [id, key] of Object.entries(map)) {
|
|
const el = $(id);
|
|
if (!el) continue;
|
|
if (id === "lblWorkspace") {
|
|
el.innerHTML = `<i class="bi bi-folder2-open"></i> ${escapeHtml(t(key))}`;
|
|
} else if (id.startsWith("lbl") && id !== "lblTtsEnabled" && id !== "lblTerminalEnabled") {
|
|
const icon = el.querySelector("i");
|
|
const iconHtml = icon ? icon.outerHTML + " " : "";
|
|
el.innerHTML = iconHtml + escapeHtml(t(key));
|
|
} else {
|
|
el.textContent = t(key);
|
|
}
|
|
}
|
|
|
|
if (terminalBadge) terminalBadge.title = t("terminalActiveTitle");
|
|
if (textInput) textInput.placeholder = t("placeholder");
|
|
if (workspaceInput) workspaceInput.placeholder = t("workspacePlaceholder");
|
|
if (micLabel && !state.isRecording && !state.responding) updateMicIdleUI();
|
|
if (audioLabel && !state.isRecording) audioLabel.textContent = t("audio");
|
|
if (ttsToggle) ttsToggle.checked = state.ttsEnabled;
|
|
if (wakeToggle) wakeToggle.checked = state.wakeEnabled;
|
|
if (uiLangSelect) uiLangSelect.value = state.uiLang;
|
|
if (terminalToggle) terminalToggle.checked = state.terminalEnabled;
|
|
if (workspaceInput) workspaceInput.value = state.workspacePath;
|
|
updateWorkspaceStatusUI();
|
|
}
|
|
|
|
function updateWorkspaceStatusUI() {
|
|
if (!workspaceStatus) return;
|
|
workspaceStatus.classList.remove("active", "error");
|
|
const active = state.terminalEnabled && state.workspacePath;
|
|
if (terminalBadge) terminalBadge.classList.toggle("visible", !!active);
|
|
if (active) {
|
|
workspaceStatus.textContent = t("workspaceActive", { path: state.workspacePath });
|
|
workspaceStatus.classList.add("active");
|
|
} else if (!workspaceStatus.classList.contains("error")) {
|
|
workspaceStatus.textContent = t("workspaceOff");
|
|
}
|
|
}
|
|
|
|
function sendWorkspaceConfig() {
|
|
if (state.ws?.readyState !== WebSocket.OPEN) return;
|
|
state.ws.send(JSON.stringify({
|
|
type: "workspace_config",
|
|
enabled: state.terminalEnabled,
|
|
path: state.workspacePath,
|
|
}));
|
|
}
|
|
|
|
// ── Boot ─────────────────────────────────────────────────────
|
|
window.addEventListener("DOMContentLoaded", async () => {
|
|
applyI18n();
|
|
initWebSocket();
|
|
initGeolocation();
|
|
initKeyboardShortcuts();
|
|
await requestPermissions();
|
|
await populateDeviceSelectors();
|
|
await initCamera(state.selectedCamId);
|
|
await initMic(state.selectedMicId);
|
|
await loadAvatar(state.selectedModel);
|
|
renderAudio();
|
|
|
|
settingsBtn.addEventListener("click", () => {
|
|
settingsPanel.classList.toggle("hidden");
|
|
settingsBtn.classList.toggle("open");
|
|
});
|
|
|
|
clearChatBtn.addEventListener("click", clearChat);
|
|
micBtn.addEventListener("click", toggleRecording);
|
|
stopBtn.addEventListener("click", cancelResponse);
|
|
|
|
sendBtn.addEventListener("click", sendText);
|
|
textInput.addEventListener("keydown", e => {
|
|
if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); sendText(); }
|
|
});
|
|
|
|
camSelect.addEventListener("change", async () => {
|
|
state.selectedCamId = camSelect.value;
|
|
await initCamera(state.selectedCamId);
|
|
});
|
|
micSelect.addEventListener("change", async () => {
|
|
state.selectedMicId = micSelect.value;
|
|
await initMic(state.selectedMicId);
|
|
});
|
|
langSelect.addEventListener("change", () => {
|
|
state.selectedLang = langSelect.value;
|
|
sendAudioConfig();
|
|
});
|
|
uiLangSelect.addEventListener("change", () => {
|
|
state.uiLang = uiLangSelect.value;
|
|
localStorage.setItem("arisu_ui_lang", state.uiLang);
|
|
applyI18n();
|
|
restoreWelcomeIfEmpty();
|
|
});
|
|
ttsToggle.addEventListener("change", () => {
|
|
state.ttsEnabled = ttsToggle.checked;
|
|
localStorage.setItem("arisu_tts", state.ttsEnabled ? "true" : "false");
|
|
sendAudioConfig();
|
|
});
|
|
if (wakeToggle) {
|
|
wakeToggle.addEventListener("change", () => {
|
|
state.wakeEnabled = wakeToggle.checked;
|
|
localStorage.setItem("arisu_wake", state.wakeEnabled ? "true" : "false");
|
|
updateMicIdleUI();
|
|
});
|
|
}
|
|
terminalToggle.addEventListener("change", () => {
|
|
state.terminalEnabled = terminalToggle.checked;
|
|
localStorage.setItem("arisu_terminal", state.terminalEnabled ? "true" : "false");
|
|
sendWorkspaceConfig();
|
|
updateWorkspaceStatusUI();
|
|
if (state.terminalEnabled) {
|
|
settingsPanel.classList.remove("hidden");
|
|
settingsBtn.classList.add("open");
|
|
}
|
|
restoreWelcomeIfEmpty();
|
|
});
|
|
workspaceInput.addEventListener("change", () => {
|
|
state.workspacePath = workspaceInput.value.trim();
|
|
localStorage.setItem("arisu_workspace", state.workspacePath);
|
|
if (state.terminalEnabled) sendWorkspaceConfig();
|
|
});
|
|
workspaceInput.addEventListener("keydown", e => {
|
|
if (e.key === "Enter") {
|
|
e.preventDefault();
|
|
workspaceInput.blur();
|
|
}
|
|
});
|
|
modelSelect.addEventListener("change", async () => {
|
|
state.selectedModel = modelSelect.value;
|
|
const name = modelSelect.options[modelSelect.selectedIndex].text;
|
|
showLoading(t("loadingNamed", { name }));
|
|
try { await state.avatar.switchModel(state.selectedModel); }
|
|
catch (e) { console.error(e); }
|
|
finally { hideLoading(); }
|
|
});
|
|
});
|
|
|
|
function initKeyboardShortcuts() {
|
|
document.addEventListener("keydown", e => {
|
|
if (e.key === "Escape" && state.responding) {
|
|
e.preventDefault();
|
|
cancelResponse();
|
|
return;
|
|
}
|
|
if (e.key === " " && document.activeElement !== textInput
|
|
&& document.activeElement !== workspaceInput && !state.responding) {
|
|
e.preventDefault();
|
|
toggleRecording();
|
|
}
|
|
});
|
|
}
|
|
|
|
// ── Avatar ────────────────────────────────────────────────────
|
|
async function loadAvatar(modelKey) {
|
|
showLoading(t("loadingModel"));
|
|
try {
|
|
state.avatar = new Live2DAvatar(avatarCanvas);
|
|
await state.avatar.load(modelKey);
|
|
hideLoading();
|
|
} catch (e) {
|
|
loadingTxt.textContent = t("loadError");
|
|
console.error("Avatar load error:", e);
|
|
}
|
|
}
|
|
function showLoading(msg) { loadingTxt.textContent = msg || "..."; loadingOverlay.style.display = "flex"; }
|
|
function hideLoading() { loadingOverlay.style.display = "none"; }
|
|
|
|
// ── WebSocket ─────────────────────────────────────────────────
|
|
function initWebSocket() {
|
|
const proto = location.protocol === "https:" ? "wss" : "ws";
|
|
const ws = new WebSocket(`${proto}://${location.host}/ws`);
|
|
state.ws = ws;
|
|
|
|
ws.onopen = () => {
|
|
setStatus(t("connected"), "green");
|
|
sendAudioConfig();
|
|
sendGeoConfig();
|
|
if (state.terminalEnabled && state.workspacePath) sendWorkspaceConfig();
|
|
};
|
|
|
|
ws.onmessage = ({ data }) => {
|
|
const msg = JSON.parse(data);
|
|
switch (msg.type) {
|
|
|
|
case "transcript":
|
|
transcriptEl.textContent = "";
|
|
setRecordingUI(false);
|
|
if (msg.text && !state.responding) {
|
|
const cleaned = stripWakeWord(msg.text);
|
|
if (!cleaned) {
|
|
setRecordingUI(false);
|
|
resumeAfterResponse();
|
|
break;
|
|
}
|
|
appendMessage("user", cleaned);
|
|
ws.send(JSON.stringify({ type: "question", text: cleaned }));
|
|
}
|
|
break;
|
|
|
|
case "no_speech":
|
|
transcriptEl.textContent = t("noAudio");
|
|
setRecordingUI(false);
|
|
setTimeout(() => { transcriptEl.textContent = ""; }, 1800);
|
|
resumeAfterResponse();
|
|
break;
|
|
|
|
case "busy":
|
|
transcriptEl.textContent = "";
|
|
setRecordingUI(false);
|
|
state.wakeChecking = false;
|
|
appendMessage("system", t("busy"));
|
|
updateMicIdleUI();
|
|
break;
|
|
|
|
case "wake_ok":
|
|
state.wakeChecking = false;
|
|
transcriptEl.textContent = "";
|
|
if (msg.text && !state.responding) {
|
|
state.wakePostBuffer = [];
|
|
appendMessage("user", msg.text);
|
|
state.wakePaused = true;
|
|
ws.send(JSON.stringify({ type: "question", text: msg.text }));
|
|
} else if (!state.responding) {
|
|
const buffered = state.wakePostBuffer.slice();
|
|
state.wakePostBuffer = [];
|
|
transcriptEl.textContent = t("wakeDetected");
|
|
startRecording({ autoStop: true, fromWake: true, prependChunks: buffered });
|
|
}
|
|
break;
|
|
|
|
case "no_wake":
|
|
state.wakeChecking = false;
|
|
state.wakePostBuffer = [];
|
|
transcriptEl.textContent = "";
|
|
updateMicIdleUI();
|
|
break;
|
|
|
|
case "thinking":
|
|
state.responding = true;
|
|
state.wakePaused = true;
|
|
resetWakeCapture();
|
|
updateResponseUI(true);
|
|
showThinking();
|
|
break;
|
|
|
|
case "chunk":
|
|
appendChunk(msg.text);
|
|
break;
|
|
|
|
case "done":
|
|
finishText();
|
|
updateResponseUI(false);
|
|
if (!state.ttsEnabled) resumeAfterResponse();
|
|
break;
|
|
|
|
case "cancelled":
|
|
finishText(true);
|
|
updateResponseUI(false);
|
|
appendMessage("system", t("cancelled"));
|
|
resumeAfterResponse();
|
|
break;
|
|
|
|
case "tts_audio":
|
|
playTTSAudio(msg.data);
|
|
break;
|
|
|
|
case "tts_error":
|
|
state.avatar?.stopSpeaking();
|
|
appendMessage("system", "⚠ " + t("ttsError", { msg: msg.text }));
|
|
resumeAfterResponse();
|
|
break;
|
|
|
|
case "history_cleared":
|
|
break;
|
|
|
|
case "tool_event":
|
|
appendToolMessage(toolLabel(msg.tool), msg.args);
|
|
break;
|
|
|
|
case "tool_result":
|
|
appendToolMessage("✓ " + toolLabel(msg.tool), msg.preview, true);
|
|
break;
|
|
|
|
case "weather_widget":
|
|
appendWeatherWidget(msg.data);
|
|
break;
|
|
|
|
case "workspace_ok":
|
|
if (msg.enabled && msg.path) {
|
|
state.workspacePath = msg.path;
|
|
if (workspaceInput) workspaceInput.value = msg.path;
|
|
localStorage.setItem("arisu_workspace", msg.path);
|
|
}
|
|
updateWorkspaceStatusUI();
|
|
break;
|
|
|
|
case "workspace_error":
|
|
if (workspaceStatus) {
|
|
workspaceStatus.textContent = t("workspaceError", { msg: msg.text });
|
|
workspaceStatus.classList.add("error");
|
|
}
|
|
appendMessage("system", t("workspaceError", { msg: msg.text }));
|
|
break;
|
|
|
|
case "error":
|
|
state.responding = false;
|
|
updateResponseUI(false);
|
|
state.avatar?.stopSpeaking();
|
|
setRecordingUI(false);
|
|
appendMessage("system", "⚠ " + msg.text);
|
|
resumeAfterResponse();
|
|
break;
|
|
}
|
|
};
|
|
|
|
ws.onclose = () => { setStatus(t("disconnected"), "red"); setTimeout(initWebSocket, 3000); };
|
|
ws.onerror = () => ws.close();
|
|
}
|
|
|
|
function sendGeoConfig() {
|
|
if (state.ws?.readyState === WebSocket.OPEN && state.geoLat != null && state.geoLon != null) {
|
|
state.ws.send(JSON.stringify({
|
|
type: "geo_config",
|
|
lat: state.geoLat,
|
|
lon: state.geoLon,
|
|
}));
|
|
}
|
|
}
|
|
|
|
function initGeolocation() {
|
|
if (!navigator.geolocation) return;
|
|
navigator.geolocation.getCurrentPosition(
|
|
pos => {
|
|
state.geoLat = pos.coords.latitude;
|
|
state.geoLon = pos.coords.longitude;
|
|
sendGeoConfig();
|
|
},
|
|
() => {},
|
|
{ enableHighAccuracy: false, timeout: 8000, maximumAge: 600_000 }
|
|
);
|
|
}
|
|
|
|
function sendAudioConfig() {
|
|
if (state.ws?.readyState === WebSocket.OPEN) {
|
|
state.ws.send(JSON.stringify({
|
|
type: "audio_config",
|
|
lang: state.selectedLang,
|
|
sample_rate: state.recordingSampleRate,
|
|
tts_enabled: state.ttsEnabled,
|
|
}));
|
|
}
|
|
}
|
|
|
|
function askText(text) {
|
|
if (!text.trim() || state.responding) return;
|
|
if (!state.ws || state.ws.readyState !== WebSocket.OPEN) {
|
|
appendMessage("system", t("noConnection"));
|
|
return;
|
|
}
|
|
appendMessage("user", text);
|
|
state.ws.send(JSON.stringify({ type: "question", text }));
|
|
}
|
|
|
|
function cancelResponse() {
|
|
if (!state.responding) return;
|
|
if (state.ttsSource) {
|
|
try { state.ttsSource.stop(); } catch (_) {}
|
|
state.ttsSource = null;
|
|
}
|
|
state.avatar?.stopSpeaking();
|
|
if (state.ws?.readyState === WebSocket.OPEN) {
|
|
state.ws.send(JSON.stringify({ type: "cancel" }));
|
|
}
|
|
}
|
|
|
|
function updateResponseUI(active) {
|
|
state.responding = active;
|
|
if (active) state.wakePaused = true;
|
|
if (stopBtn) stopBtn.classList.toggle("hidden", !active);
|
|
if (micBtn) micBtn.disabled = active;
|
|
if (sendBtn) sendBtn.disabled = active;
|
|
if (textInput) textInput.disabled = active;
|
|
}
|
|
|
|
function clearChat() {
|
|
if (state.responding) cancelResponse();
|
|
chatMessages.innerHTML = "";
|
|
restoreWelcomeIfEmpty();
|
|
if (state.ws?.readyState === WebSocket.OPEN) {
|
|
state.ws.send(JSON.stringify({ type: "clear_history" }));
|
|
}
|
|
}
|
|
|
|
function restoreWelcomeIfEmpty() {
|
|
if (chatMessages.children.length) return;
|
|
const div = document.createElement("div");
|
|
div.className = "welcome";
|
|
div.id = "welcomeMsg";
|
|
div.innerHTML = `
|
|
<div class="welcome-glyph">✨</div>
|
|
<strong>${escapeHtml(t("welcomeTitle"))}</strong>
|
|
<p>${state.terminalEnabled ? t("welcomeBodyTerminal") : t("welcomeBody")}</p>`;
|
|
chatMessages.appendChild(div);
|
|
}
|
|
|
|
function toolLabel(name) {
|
|
const map = {
|
|
web_search: "toolWebSearch",
|
|
list_directory: "toolListDir",
|
|
read_file: "toolReadFile",
|
|
write_file: "toolWriteFile",
|
|
create_directory: "toolMkdir",
|
|
run_command: "toolRunCmd",
|
|
get_weather: "toolWeather",
|
|
};
|
|
const key = map[name];
|
|
return key ? t(key) : name;
|
|
}
|
|
|
|
function appendToolMessage(title, args, isResult = false) {
|
|
document.getElementById("welcomeMsg")?.remove();
|
|
const div = document.createElement("div");
|
|
div.className = "msg msg-tool";
|
|
let body = `<span class="tool-name">${escapeHtml(title)}</span>`;
|
|
if (args) {
|
|
const short = args.length > 200 ? args.slice(0, 200) + "…" : args;
|
|
body += `<br>${escapeHtml(short)}`;
|
|
}
|
|
div.innerHTML = body;
|
|
chatMessages.appendChild(div);
|
|
chatMessages.scrollTop = chatMessages.scrollHeight;
|
|
}
|
|
|
|
function appendWeatherWidget(data) {
|
|
if (!data) return;
|
|
document.getElementById("welcomeMsg")?.remove();
|
|
|
|
const iconKey = data.icon || "cloudy";
|
|
const iconCls = WEATHER_ICONS[iconKey] || "bi-cloud-fill";
|
|
const temp = data.temp != null ? `${data.temp}${data.unit || "°C"}` : "—";
|
|
const tmin = data.tempMin != null ? `${data.tempMin}°` : "—";
|
|
const tmax = data.tempMax != null ? `${data.tempMax}°` : "—";
|
|
|
|
const div = document.createElement("div");
|
|
div.className = "msg msg-weather";
|
|
div.innerHTML = `
|
|
<div class="weather-card">
|
|
<div class="weather-card-header">
|
|
<i class="bi ${iconCls} weather-icon"></i>
|
|
<div class="weather-main">
|
|
<span class="weather-temp">${escapeHtml(temp)}</span>
|
|
<span class="weather-desc">${escapeHtml(data.description || "")}</span>
|
|
</div>
|
|
</div>
|
|
<div class="weather-location">
|
|
<i class="bi bi-geo-alt-fill"></i>
|
|
${escapeHtml(data.location || "")}
|
|
</div>
|
|
<div class="weather-meta">
|
|
<span><i class="bi bi-arrow-down-up"></i> ${escapeHtml(tmin)} / ${escapeHtml(tmax)}</span>
|
|
<span><i class="bi bi-droplet-fill"></i> ${escapeHtml(String(data.humidity ?? "—"))}%</span>
|
|
<span><i class="bi bi-wind"></i> ${escapeHtml(String(data.wind ?? "—"))} ${escapeHtml(t("weatherUnitWind"))}</span>
|
|
</div>
|
|
<div class="weather-date">${escapeHtml(t("weatherToday"))} · ${escapeHtml(data.date || "")}</div>
|
|
</div>`;
|
|
chatMessages.appendChild(div);
|
|
chatMessages.scrollTop = chatMessages.scrollHeight;
|
|
}
|
|
|
|
// ── TTS Audio playback ───────────────────────────────────────
|
|
async function playTTSAudio(base64mp3) {
|
|
if (!state.ttsEnabled) return;
|
|
|
|
if (state.ttsSource) {
|
|
try { state.ttsSource.stop(); } catch (_) {}
|
|
state.ttsSource = null;
|
|
}
|
|
|
|
try {
|
|
const binary = atob(base64mp3);
|
|
const bytes = new Uint8Array(binary.length);
|
|
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
|
|
const ctx = state.audioCtx || new (window.AudioContext || window.webkitAudioContext)();
|
|
if (!state.audioCtx) state.audioCtx = ctx;
|
|
if (ctx.state === "suspended") await ctx.resume();
|
|
|
|
const audioBuffer = await ctx.decodeAudioData(bytes.buffer);
|
|
const source = ctx.createBufferSource();
|
|
source.buffer = audioBuffer;
|
|
|
|
const analyser = ctx.createAnalyser();
|
|
analyser.fftSize = 256;
|
|
source.connect(analyser);
|
|
analyser.connect(ctx.destination);
|
|
|
|
state.avatar?.startSpeaking(analyser);
|
|
source.onended = () => {
|
|
state.avatar?.stopSpeaking();
|
|
state.ttsSource = null;
|
|
resumeAfterResponse();
|
|
};
|
|
source.start(0);
|
|
state.ttsSource = source;
|
|
|
|
} catch (e) {
|
|
console.error("TTS playback error:", e);
|
|
state.avatar?.stopSpeaking();
|
|
appendMessage("system", "⚠ " + t("audioError", { msg: e.message }));
|
|
}
|
|
}
|
|
|
|
// ── Permissions & Devices ────────────────────────────────────
|
|
async function requestPermissions() {
|
|
try {
|
|
const s = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
|
|
s.getTracks().forEach(t => t.stop());
|
|
} catch (e) { console.warn("Permissions:", e); }
|
|
}
|
|
|
|
async function populateDeviceSelectors() {
|
|
try {
|
|
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
fillSelect(camSelect, devices.filter(d => d.kind === "videoinput"), t("camera"));
|
|
fillSelect(micSelect, devices.filter(d => d.kind === "audioinput"), t("microphone"));
|
|
state.selectedCamId = camSelect.value || null;
|
|
state.selectedMicId = micSelect.value || null;
|
|
} catch (e) { console.warn("enumerateDevices:", e); }
|
|
}
|
|
|
|
function fillSelect(sel, devices, label) {
|
|
sel.innerHTML = "";
|
|
if (!devices.length) {
|
|
sel.innerHTML = `<option value="">${t("noDevice", { label })}</option>`;
|
|
return;
|
|
}
|
|
devices.forEach((d, i) => {
|
|
const opt = document.createElement("option");
|
|
opt.value = d.deviceId;
|
|
opt.textContent = d.label || t("deviceLabel", { label, n: i + 1 });
|
|
sel.appendChild(opt);
|
|
});
|
|
}
|
|
|
|
// ── Camera / FaceMesh ────────────────────────────────────────
|
|
async function initCamera(deviceId) {
|
|
if (state.camera) { try { state.camera.stop(); } catch (_) {} state.camera = null; }
|
|
if (state.camStream) { state.camStream.getTracks().forEach(t => t.stop()); state.camStream = null; }
|
|
|
|
try {
|
|
const stream = await navigator.mediaDevices.getUserMedia({
|
|
video: deviceId ? { deviceId: { exact: deviceId } } : true,
|
|
});
|
|
state.camStream = stream;
|
|
webcamVideo.srcObject = stream;
|
|
await webcamVideo.play();
|
|
|
|
if (!state.faceMesh) {
|
|
const mesh = new FaceMesh({
|
|
locateFile: f => `https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh@0.4/${f}`
|
|
});
|
|
mesh.setOptions({ maxNumFaces: 1, refineLandmarks: true,
|
|
minDetectionConfidence: 0.5, minTrackingConfidence: 0.5 });
|
|
mesh.onResults(onFaceResults);
|
|
state.faceMesh = mesh;
|
|
}
|
|
const cam = new Camera(webcamVideo, {
|
|
onFrame: async () => { if (state.faceMesh) await state.faceMesh.send({ image: webcamVideo }); },
|
|
width: 320, height: 240,
|
|
});
|
|
cam.start();
|
|
state.camera = cam;
|
|
faceStatus.textContent = t("cameraActive");
|
|
} catch (e) {
|
|
console.warn("Camera:", e);
|
|
faceStatus.textContent = t("noCamera");
|
|
}
|
|
}
|
|
|
|
function onFaceResults(results) {
|
|
if (!results.multiFaceLandmarks?.length) {
|
|
state.avatar?.clearLook();
|
|
faceStatus.textContent = t("noFace");
|
|
return;
|
|
}
|
|
faceStatus.textContent = t("faceDetected");
|
|
const lm = results.multiFaceLandmarks[0];
|
|
const nx = lm.length > 468 ? (lm[468].x + lm[473].x) / 2 : lm[1].x;
|
|
const ny = lm.length > 468 ? (lm[468].y + lm[473].y) / 2 : lm[1].y;
|
|
state.avatar?.setLook((0.5 - nx) * 1.6, (ny - 0.42) * 1.4);
|
|
}
|
|
|
|
// ── Microphone ───────────────────────────────────────────────
|
|
async function initMic(deviceId) {
|
|
teardownContinuousAudio();
|
|
if (state.audioStream) { state.audioStream.getTracks().forEach(t => t.stop()); state.audioStream = null; }
|
|
if (state.audioCtx) { try { await state.audioCtx.close(); } catch (_) {} state.audioCtx = null; }
|
|
|
|
try {
|
|
const stream = await navigator.mediaDevices.getUserMedia({
|
|
audio: deviceId
|
|
? { deviceId: { exact: deviceId }, echoCancellation: true, noiseSuppression: true }
|
|
: { echoCancellation: true, noiseSuppression: true },
|
|
video: false,
|
|
});
|
|
state.audioStream = stream;
|
|
|
|
const ctx = new (window.AudioContext || window.webkitAudioContext)();
|
|
state.audioCtx = ctx;
|
|
state.recordingSampleRate = ctx.sampleRate;
|
|
sendAudioConfig();
|
|
|
|
const src = ctx.createMediaStreamSource(stream);
|
|
state.audioSource = src;
|
|
|
|
const analyser = ctx.createAnalyser();
|
|
analyser.fftSize = 128;
|
|
src.connect(analyser);
|
|
state.analyser = analyser;
|
|
state.audioData = new Uint8Array(analyser.frequencyBinCount);
|
|
|
|
const gain = ctx.createGain();
|
|
gain.gain.value = 0;
|
|
gain.connect(ctx.destination);
|
|
state.silentGain = gain;
|
|
|
|
resetVAD();
|
|
resetWakeCapture();
|
|
setupContinuousAudio();
|
|
|
|
} catch (e) {
|
|
console.warn("Mic:", e);
|
|
appendMessage("system", "⚠ " + t("micUnavailable", { msg: e.message }));
|
|
}
|
|
}
|
|
|
|
// ── Continuous audio (Safari + Chrome) ───────────────────────
|
|
function canWakeListen() {
|
|
return state.wakeEnabled && state.listenActive && !state.isRecording
|
|
&& !state.responding && !state.wakePaused && !state.wakeChecking;
|
|
}
|
|
|
|
function setupContinuousAudio() {
|
|
if (!state.audioCtx || !state.audioSource || state.continuousProcessor) return;
|
|
const sp = state.audioCtx.createScriptProcessor(4096, 1, 1);
|
|
sp.onaudioprocess = onContinuousAudio;
|
|
state.audioSource.connect(sp);
|
|
sp.connect(state.silentGain);
|
|
state.continuousProcessor = sp;
|
|
state.listenActive = true;
|
|
updateMicIdleUI();
|
|
}
|
|
|
|
function teardownContinuousAudio() {
|
|
if (state.continuousProcessor) {
|
|
try { state.continuousProcessor.disconnect(); } catch (_) {}
|
|
state.continuousProcessor = null;
|
|
}
|
|
state.listenActive = false;
|
|
resetWakeCapture();
|
|
}
|
|
|
|
function onContinuousAudio(e) {
|
|
const chunk = new Float32Array(e.inputBuffer.getChannelData(0));
|
|
|
|
if (state.isRecording) {
|
|
state.pcmChunks.push(chunk);
|
|
if (state.recordingAutoStop) processVAD(chunk);
|
|
return;
|
|
}
|
|
|
|
if (state.wakeChecking) {
|
|
pushWakePostBuffer(chunk);
|
|
return;
|
|
}
|
|
|
|
if (!state.wakeEnabled || state.wakePaused || state.responding) return;
|
|
|
|
if (!state.wakeCaptureActive) {
|
|
state.wakePreRoll.push(chunk);
|
|
trimWakePreRoll();
|
|
}
|
|
|
|
processWakeListen(chunk);
|
|
}
|
|
|
|
function chunkRms(buf) {
|
|
let sum = 0;
|
|
for (let i = 0; i < buf.length; i++) sum += buf[i] * buf[i];
|
|
return Math.sqrt(sum / buf.length);
|
|
}
|
|
|
|
function chunkMsFrom(buf) {
|
|
return (buf.length / state.recordingSampleRate) * 1000;
|
|
}
|
|
|
|
function mergeFloat32(chunks) {
|
|
const total = chunks.reduce((s, c) => s + c.length, 0);
|
|
const out = new Float32Array(total);
|
|
let off = 0;
|
|
chunks.forEach(c => { out.set(c, off); off += c.length; });
|
|
return out;
|
|
}
|
|
|
|
function arrayBufferToBase64(buffer) {
|
|
const bytes = new Uint8Array(buffer);
|
|
let bin = "";
|
|
for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]);
|
|
return btoa(bin);
|
|
}
|
|
|
|
function resetWakeCapture() {
|
|
state.wakeCaptureActive = false;
|
|
state.wakeChunks = [];
|
|
state.wakeHadSpeech = false;
|
|
state.wakeSilenceMs = 0;
|
|
state.wakeTotalMs = 0;
|
|
state.wakePendingMs = 0;
|
|
}
|
|
|
|
function trimWakePreRoll() {
|
|
const maxMs = WAKE_PREROLL_MS;
|
|
let total = 0;
|
|
for (let i = state.wakePreRoll.length - 1; i >= 0; i--) {
|
|
total += chunkMsFrom(state.wakePreRoll[i]);
|
|
if (total > maxMs) {
|
|
state.wakePreRoll = state.wakePreRoll.slice(i + 1);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
function pushWakePostBuffer(chunk) {
|
|
state.wakePostBuffer.push(chunk);
|
|
const maxMs = WAKE_POST_BUFFER_MS;
|
|
let total = 0;
|
|
for (let i = state.wakePostBuffer.length - 1; i >= 0; i--) {
|
|
total += chunkMsFrom(state.wakePostBuffer[i]);
|
|
if (total > maxMs) {
|
|
state.wakePostBuffer = state.wakePostBuffer.slice(i + 1);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
function seedVADFromChunks(chunks, resetSilence = false) {
|
|
resetVAD();
|
|
for (const chunk of chunks) {
|
|
const rms = chunkRms(chunk);
|
|
const chunkMs = chunkMsFrom(chunk);
|
|
state.vadTotalMs += chunkMs;
|
|
if (rms >= VAD_SPEECH_RMS) {
|
|
state.vadHadSpeech = true;
|
|
state.vadSpeechMs += chunkMs;
|
|
state.vadSilenceMs = 0;
|
|
} else if (state.vadHadSpeech) {
|
|
state.vadSilenceMs += chunkMs;
|
|
}
|
|
}
|
|
if (resetSilence && state.vadHadSpeech) {
|
|
state.vadSilenceMs = 0;
|
|
}
|
|
}
|
|
|
|
function beginWakeCapture(firstChunk) {
|
|
state.wakeCaptureActive = true;
|
|
state.wakeChunks = state.wakePreRoll.slice();
|
|
state.wakePreRoll = [];
|
|
if (!state.wakeChunks.length) state.wakeChunks.push(firstChunk);
|
|
state.wakeHadSpeech = true;
|
|
state.wakeSilenceMs = 0;
|
|
state.wakeTotalMs = state.wakeChunks.reduce((sum, c) => sum + chunkMsFrom(c), 0);
|
|
setWakeCaptureUI(true);
|
|
}
|
|
|
|
function setWakeCaptureUI(active) {
|
|
if (!micBtn || !micLabel) return;
|
|
if (active) {
|
|
micBtn.classList.add("active");
|
|
micBtn.classList.remove("wake-listen");
|
|
micLabel.textContent = t("wakeListening");
|
|
if (micDot) micDot.style.background = "#f59e0b";
|
|
audioLabel.textContent = t("recording");
|
|
} else if (!state.isRecording) {
|
|
updateMicIdleUI();
|
|
}
|
|
}
|
|
|
|
function processWakeListen(chunk) {
|
|
const rms = chunkRms(chunk);
|
|
const ms = chunkMsFrom(chunk);
|
|
|
|
if (state.wakeCaptureActive) {
|
|
state.wakeChunks.push(chunk);
|
|
state.wakeTotalMs += ms;
|
|
if (rms >= VAD_SPEECH_RMS) {
|
|
state.wakeHadSpeech = true;
|
|
state.wakeSilenceMs = 0;
|
|
} else if (state.wakeHadSpeech) {
|
|
state.wakeSilenceMs += ms;
|
|
}
|
|
if (state.wakeHadSpeech && state.wakeSilenceMs >= WAKE_SILENCE_MS) {
|
|
submitWakeCheck();
|
|
} else if (state.wakeTotalMs >= WAKE_MAX_MS) {
|
|
submitWakeCheck();
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (rms >= VAD_SPEECH_RMS) {
|
|
state.wakePendingMs += ms;
|
|
if (state.wakePendingMs >= WAKE_DEBOUNCE_MS) {
|
|
beginWakeCapture(chunk);
|
|
}
|
|
} else {
|
|
state.wakePendingMs = 0;
|
|
}
|
|
}
|
|
|
|
function submitWakeCheck() {
|
|
const chunks = state.wakeChunks.slice();
|
|
resetWakeCapture();
|
|
setWakeCaptureUI(false);
|
|
state.wakePreRoll = [];
|
|
|
|
const combined = mergeFloat32(chunks);
|
|
if (chunkRms(combined) < 0.012) {
|
|
updateMicIdleUI();
|
|
return;
|
|
}
|
|
|
|
const pcm16 = float32ToInt16(downsample(combined, state.recordingSampleRate, 16000));
|
|
if (pcm16.length < 4_000) {
|
|
updateMicIdleUI();
|
|
return;
|
|
}
|
|
|
|
if (state.ws?.readyState !== WebSocket.OPEN) return;
|
|
|
|
state.wakeChecking = true;
|
|
state.wakePostBuffer = [];
|
|
transcriptEl.textContent = t("wakeChecking");
|
|
state.ws.send(JSON.stringify({
|
|
type: "wake_check",
|
|
pcm: arrayBufferToBase64(pcm16.buffer),
|
|
}));
|
|
}
|
|
|
|
function stripWakeWord(text) {
|
|
return (text || "")
|
|
.replace(/^\s*(arisu|ariso|alice|alisa|アリス)\s*[,.:!\-]?\s*/i, "")
|
|
.trim();
|
|
}
|
|
|
|
function resumeAfterResponse() {
|
|
state.wakePaused = false;
|
|
state.wakeChecking = false;
|
|
setTimeout(updateMicIdleUI, 400);
|
|
}
|
|
|
|
function resetVAD() {
|
|
state.vadHadSpeech = false;
|
|
state.vadSilenceMs = 0;
|
|
state.vadSpeechMs = 0;
|
|
state.vadTotalMs = 0;
|
|
}
|
|
|
|
function updateMicIdleUI() {
|
|
if (!micBtn || !micLabel) return;
|
|
micBtn.classList.remove("active");
|
|
if (state.isRecording || state.wakeChecking) return;
|
|
if (canWakeListen()) {
|
|
micBtn.classList.add("wake-listen");
|
|
micLabel.textContent = t("wakeListen");
|
|
if (micDot) micDot.style.background = "#22d3ee";
|
|
} else {
|
|
micBtn.classList.remove("wake-listen");
|
|
micLabel.textContent = t("speak");
|
|
if (micDot) micDot.style.background = "#4ade80";
|
|
}
|
|
}
|
|
|
|
function processVAD(float32Chunk) {
|
|
const rms = chunkRms(float32Chunk);
|
|
const chunkMs = chunkMsFrom(float32Chunk);
|
|
|
|
state.vadTotalMs += chunkMs;
|
|
if (rms >= VAD_SPEECH_RMS) {
|
|
state.vadHadSpeech = true;
|
|
state.vadSilenceMs = 0;
|
|
state.vadSpeechMs += chunkMs;
|
|
} else if (state.vadHadSpeech) {
|
|
state.vadSilenceMs += chunkMs;
|
|
}
|
|
|
|
if (state.vadHadSpeech && state.vadSpeechMs >= VAD_MIN_SPEECH_MS
|
|
&& state.vadSilenceMs >= VAD_SILENCE_MS) {
|
|
stopRecording();
|
|
return;
|
|
}
|
|
if (state.vadTotalMs >= VAD_MAX_RECORD_MS) stopRecording();
|
|
}
|
|
|
|
// ── Recording ────────────────────────────────────────────────
|
|
function toggleRecording() {
|
|
if (state.responding) return;
|
|
if (state.isRecording) stopRecording();
|
|
else startRecording({ autoStop: false, fromWake: false });
|
|
}
|
|
|
|
function startRecording({ autoStop = false, fromWake = false, prependChunks = [] } = {}) {
|
|
if (state.responding) return;
|
|
if (!state.audioCtx || !state.continuousProcessor) {
|
|
appendMessage("system", "⚠ " + t("micNotReady"));
|
|
return;
|
|
}
|
|
resetWakeCapture();
|
|
state.wakeChecking = false;
|
|
state.wakePreRoll = [];
|
|
|
|
state.isRecording = true;
|
|
state.recordingAutoStop = autoStop;
|
|
state.pcmChunks = prependChunks.length ? prependChunks.slice() : [];
|
|
seedVADFromChunks(prependChunks, fromWake);
|
|
|
|
setRecordingUI(true, fromWake ? "wake" : "manual");
|
|
if (!fromWake) transcriptEl.textContent = "";
|
|
|
|
if (fromWake && prependChunks.length && state.vadHadSpeech
|
|
&& state.vadSpeechMs >= VAD_MIN_SPEECH_MS
|
|
&& state.vadSilenceMs >= VAD_SILENCE_MS) {
|
|
setTimeout(() => stopRecording(), 0);
|
|
}
|
|
}
|
|
|
|
function stopRecording() {
|
|
if (!state.isRecording) return;
|
|
state.isRecording = false;
|
|
state.recordingAutoStop = false;
|
|
|
|
if (!state.pcmChunks.length) {
|
|
setRecordingUI(false);
|
|
updateMicIdleUI();
|
|
return;
|
|
}
|
|
|
|
const combined = mergeFloat32(state.pcmChunks);
|
|
state.pcmChunks = [];
|
|
resetVAD();
|
|
|
|
if (chunkRms(combined) < 0.015) {
|
|
transcriptEl.textContent = t("noVoice");
|
|
setRecordingUI(false);
|
|
setTimeout(() => { transcriptEl.textContent = ""; updateMicIdleUI(); }, 1500);
|
|
return;
|
|
}
|
|
|
|
const pcm16 = float32ToInt16(downsample(combined, state.recordingSampleRate, 16000));
|
|
|
|
if (state.ws?.readyState === WebSocket.OPEN && !state.responding) {
|
|
transcriptEl.textContent = t("transcribing");
|
|
state.wakePaused = true;
|
|
state.ws.send(pcm16.buffer);
|
|
} else {
|
|
setRecordingUI(false);
|
|
updateMicIdleUI();
|
|
}
|
|
}
|
|
|
|
function setRecordingUI(active, mode = "manual") {
|
|
micBtn.classList.toggle("active", active);
|
|
micBtn.classList.toggle("wake-listen", !active && canWakeListen());
|
|
if (micIcon) micIcon.className = active ? "bi bi-stop-fill" : "bi bi-mic-fill";
|
|
if (active) {
|
|
micLabel.textContent = mode === "wake" ? t("wakeListening") : t("stop");
|
|
micDot.style.background = "#ef4444";
|
|
audioLabel.textContent = t("recording");
|
|
} else {
|
|
updateMicIdleUI();
|
|
audioLabel.textContent = t("audio");
|
|
}
|
|
}
|
|
|
|
// ── PCM helpers ───────────────────────────────────────────────
|
|
function downsample(buf, from, to) {
|
|
if (from === to) return buf;
|
|
const ratio = from / to;
|
|
const len = Math.floor(buf.length / ratio);
|
|
const result = new Float32Array(len);
|
|
for (let i = 0; i < len; i++) {
|
|
const s = Math.floor(i * ratio);
|
|
const e = Math.min(Math.floor((i + 1) * ratio), buf.length);
|
|
let sum = 0;
|
|
for (let j = s; j < e; j++) sum += buf[j];
|
|
result[i] = sum / (e - s);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function float32ToInt16(buf) {
|
|
const out = new Int16Array(buf.length);
|
|
for (let i = 0; i < buf.length; i++) {
|
|
const s = Math.max(-1, Math.min(1, buf[i]));
|
|
out[i] = s < 0 ? s * 0x8000 : s * 0x7FFF;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// ── Audio visualizer ──────────────────────────────────────────
|
|
function renderAudio() {
|
|
requestAnimationFrame(renderAudio);
|
|
if (!state.analyser || !state.audioData) return;
|
|
const ctx = audioCanvas.getContext("2d");
|
|
const W = audioCanvas.width, H = audioCanvas.height;
|
|
state.analyser.getByteFrequencyData(state.audioData);
|
|
ctx.clearRect(0, 0, W, H);
|
|
const bars = state.audioData.length;
|
|
const bw = W / bars - 1;
|
|
for (let i = 0; i < bars; i++) {
|
|
const val = state.audioData[i] / 255;
|
|
const bh = val * (H - 4);
|
|
const hue = state.isRecording ? val * 30 : canWakeListen() ? 180 + val * 40 : 260 + val * 60;
|
|
ctx.fillStyle = `hsla(${hue},90%,65%,${0.5 + val * 0.5})`;
|
|
ctx.beginPath();
|
|
ctx.roundRect(i * (bw + 1), H - bh - 2, bw, bh + 2, 2);
|
|
ctx.fill();
|
|
}
|
|
}
|
|
|
|
// ── Text input ────────────────────────────────────────────────
|
|
function sendText() {
|
|
const text = textInput.value.trim();
|
|
if (!text) return;
|
|
textInput.value = "";
|
|
askText(text);
|
|
}
|
|
|
|
// ── Chat UI ───────────────────────────────────────────────────
|
|
let currentBubble = null;
|
|
let fullResponse = "";
|
|
|
|
function appendMessage(role, text) {
|
|
document.getElementById("welcomeMsg")?.remove();
|
|
const div = document.createElement("div");
|
|
div.className = `msg msg-${role}`;
|
|
if (role === "user") div.innerHTML = `<span class="msg-label">${escapeHtml(t("you"))}</span><span class="msg-text">${escapeHtml(text)}</span>`;
|
|
else if (role === "yuki") div.innerHTML = `<span class="msg-label">${escapeHtml(t("arisu"))}</span><span class="msg-text">${escapeHtml(text)}</span>`;
|
|
else div.innerHTML = `<span class="msg-text msg-system">${escapeHtml(text)}</span>`;
|
|
chatMessages.appendChild(div);
|
|
chatMessages.scrollTop = chatMessages.scrollHeight;
|
|
return div;
|
|
}
|
|
|
|
function showThinking() {
|
|
document.getElementById("welcomeMsg")?.remove();
|
|
fullResponse = "";
|
|
currentBubble = document.createElement("div");
|
|
currentBubble.className = "msg msg-yuki";
|
|
currentBubble.innerHTML = `<span class="msg-label">${escapeHtml(t("arisu"))}</span><span class="msg-text thinking"><span class="dot"></span><span class="dot"></span><span class="dot"></span></span>`;
|
|
chatMessages.appendChild(currentBubble);
|
|
chatMessages.scrollTop = chatMessages.scrollHeight;
|
|
}
|
|
|
|
function stripAudioTags(text) {
|
|
return text.replace(/\[[^\]]{1,40}\]/g, " ").replace(/\s{2,}/g, " ").trim();
|
|
}
|
|
|
|
const KNOWN_TAGS = new Set(["laughs","giggles","excited","whispers","sighs","gasps","crying","sarcastic"]);
|
|
|
|
function detectExpressions(text) {
|
|
const matches = text.matchAll(/\[([^\]]{1,40})\]/g);
|
|
for (const m of matches) {
|
|
const tag = m[1].toLowerCase();
|
|
if (KNOWN_TAGS.has(tag)) state.avatar?.setExpression(tag);
|
|
}
|
|
}
|
|
|
|
function appendChunk(text) {
|
|
if (!currentBubble) return;
|
|
detectExpressions(text);
|
|
fullResponse += text;
|
|
currentBubble.querySelector(".msg-text").textContent = stripAudioTags(fullResponse);
|
|
chatMessages.scrollTop = chatMessages.scrollHeight;
|
|
}
|
|
|
|
function finishText(cancelled = false) {
|
|
if (!cancelled) {
|
|
state.avatar?.setHappy(0.8);
|
|
setTimeout(() => state.avatar?.setHappy(0), 4000);
|
|
}
|
|
currentBubble = null;
|
|
fullResponse = "";
|
|
}
|
|
|
|
// ── Helpers ───────────────────────────────────────────────────
|
|
function setStatus(txt, color) {
|
|
statusTxt.textContent = txt;
|
|
statusDot.style.background = color === "green" ? "#22c55e" : color === "red" ? "#ef4444" : "#f59e0b";
|
|
}
|
|
function escapeHtml(str) {
|
|
return str.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")
|
|
.replace(/"/g,""").replace(/\n/g,"<br>");
|
|
}
|
|
|
|
// Estado inicial de conexión
|
|
setStatus(t("connecting"), "amber");
|