Build streaming voice assistant for Mini 2

This commit is contained in:
2026-11-10 15:21:59 +03:00
commit 2d7d27fac3
27 changed files with 1701 additions and 0 deletions
+109
View File
@@ -0,0 +1,109 @@
const telegram = window.Telegram?.WebApp;
const status = document.querySelector('#status');
const admin = document.querySelector('#admin');
const history = document.querySelector('#history');
const settings = document.querySelector('#settings');
const showHistory = document.querySelector('#show-history');
const showSettings = document.querySelector('#show-settings');
const clear = document.querySelector('#clear');
const form = document.querySelector('#form');
const turns = document.querySelector('#turns');
const dateFormat = new Intl.DateTimeFormat('ru', { dateStyle: 'medium', timeStyle: 'short' });
async function api(path = '', options = {}) {
const response = await fetch('/api/admin' + path, {
...options,
headers: { Authorization: 'tma ' + telegram.initData, 'Content-Type': 'application/json' },
});
if (response.status === 401) throw new Error('Доступ открыт владельцу. Если сессия истекла, заново открой приложение из меню бота.');
if (response.status === 400) throw new Error('Проверь ID модели, голос и системный промпт.');
if (!response.ok) throw new Error('Сервер недоступен. Попробуй ещё раз.');
return response.json();
}
function renderTurns(items) {
turns.replaceChildren();
clear.disabled = items.length === 0;
if (!items.length) {
const empty = document.createElement('p');
empty.className = 'empty';
empty.textContent = 'Здесь появятся последние ответы станции.';
turns.append(empty);
}
for (const turn of items) {
const article = document.createElement('article');
const date = document.createElement('time');
date.dateTime = turn.createdAt;
date.textContent = dateFormat.format(new Date(turn.createdAt));
const question = document.createElement('p');
question.className = 'question';
question.textContent = turn.user;
const answer = document.createElement('p');
answer.textContent = turn.assistant;
article.append(date, question, answer);
turns.append(article);
}
}
function selectTab(isHistory) {
showHistory.setAttribute('aria-pressed', String(isHistory));
showSettings.setAttribute('aria-pressed', String(!isHistory));
history.hidden = !isHistory;
settings.hidden = isHistory;
status.textContent = '';
}
showHistory.onclick = async () => {
selectTab(true);
try {
renderTurns((await api()).history);
} catch (error) {
status.textContent = error.message;
}
};
showSettings.onclick = () => selectTab(false);
form.onsubmit = async (event) => {
event.preventDefault();
const button = form.querySelector('button');
button.disabled = true;
try {
const body = JSON.stringify(Object.fromEntries(new FormData(form)));
await api('/settings', { method: 'PUT', body });
status.textContent = 'Сохранено. Настройки вступят в силу со следующего запроса.';
} catch (error) {
status.textContent = error.message;
} finally {
button.disabled = false;
}
};
clear.onclick = () => {
const erase = async (confirmed) => {
if (!confirmed) return;
clear.disabled = true;
try {
renderTurns((await api('/history', { method: 'DELETE' })).history);
} catch (error) {
status.textContent = error.message;
clear.disabled = false;
}
};
if (telegram.isVersionAtLeast('6.2')) telegram.showConfirm('Очистить историю разговоров?', erase);
else erase(window.confirm('Очистить историю разговоров?'));
};
telegram?.ready();
if (!telegram?.initData) {
status.textContent = 'Открой пульт из меню Telegram-бота.';
} else {
try {
const data = await api();
for (const [name, value] of Object.entries(data.settings)) form.elements.namedItem(name).value = value;
renderTurns(data.history);
status.textContent = '';
admin.hidden = false;
} catch (error) {
status.textContent = error.message;
}
}
+46
View File
@@ -0,0 +1,46 @@
<!doctype html>
<html lang="ru">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="color-scheme" content="light dark">
<title>Алиса · пульт</title>
<link rel="stylesheet" href="/style.css">
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<script type="module" src="/app.js"></script>
</head>
<body>
<main>
<header>
<div class="badge" aria-hidden="true"><i></i><i></i><i></i><i></i><i></i></div>
<div><p class="eyebrow">СТАНЦИЯ МИНИ 2</p><h1>Пульт Алисы</h1></div>
</header>
<p id="status" role="status">Подключаемся…</p>
<div id="admin" hidden>
<nav aria-label="Разделы">
<button id="show-history" type="button" aria-pressed="true">История</button>
<button id="show-settings" type="button" aria-pressed="false">Настройки</button>
</nav>
<section id="history">
<div class="section-title"><h2>Разговоры</h2><button id="clear" type="button" class="quiet">Очистить</button></div>
<div id="turns"></div>
</section>
<section id="settings" hidden>
<h2>Голосовой тракт</h2>
<form id="form">
<label>Распознавание<select name="sttModel"><option value="scribe_v2_realtime">Scribe v2 Realtime</option></select></label>
<label>Модель OpenRouter<input name="llmModel" required maxlength="120" spellcheck="false" autocomplete="off"></label>
<label>Синтез речи<select name="ttsModel">
<option value="eleven_v4_turbo">Eleven v4 Turbo</option>
<option value="eleven_v3_conversational">Eleven v3 Conversational</option>
<option value="eleven_flash_v2_5">Eleven Flash v2.5</option>
</select></label>
<label>ID голоса ElevenLabs<input name="voiceId" required minlength="20" maxlength="20" spellcheck="false" autocomplete="off"></label>
<label>Системный промпт<textarea name="systemPrompt" required maxlength="4000" rows="6"></textarea></label>
<button class="primary">Сохранить</button>
</form>
</section>
</div>
</main>
</body>
</html>
+41
View File
@@ -0,0 +1,41 @@
:root {
color-scheme: light dark;
font: 16px/1.45 system-ui, sans-serif;
color: var(--tg-theme-text-color, light-dark(#181724, #f4f1ff));
background: var(--tg-theme-bg-color, light-dark(#faf8ff, #171421));
--muted: var(--tg-theme-hint-color, light-dark(#645e76, #afa7c0));
--surface: var(--tg-theme-secondary-bg-color, light-dark(#eeeaf7, #242031));
--accent: var(--tg-theme-button-color, light-dark(#6636d2, #8b61ef));
--accent-text: var(--tg-theme-button-text-color, white);
}
* { box-sizing: border-box; }
body { margin: 0; }
main { max-width: 640px; margin: auto; padding: max(26px, env(safe-area-inset-top)) 20px max(36px, env(safe-area-inset-bottom)); }
header { display: flex; gap: 16px; align-items: center; margin-bottom: 20px; }
.badge { width: 54px; height: 54px; border-radius: 18px; background: var(--accent); display: flex; align-items: center; justify-content: center; gap: 3px; }
.badge i { width: 3px; height: 11px; background: var(--accent-text); border-radius: 4px; }
.badge i:nth-child(even) { height: 19px; }
.badge i:nth-child(3) { height: 27px; }
h1, h2, p { margin: 0; }
h1 { font-size: 27px; letter-spacing: -.04em; }
h2 { font-size: 20px; }
.eyebrow { font-size: 10px; font-weight: 800; letter-spacing: .19em; color: var(--muted); }
#status { color: var(--muted); margin-bottom: 14px; }
#status:empty { display: none; }
nav { display: flex; gap: 4px; border-radius: 15px; padding: 4px; background: var(--surface); margin-bottom: 22px; }
button, input, select, textarea { font: inherit; color: inherit; }
button { min-height: 42px; padding: 8px 13px; border: 0; border-radius: 11px; background: transparent; cursor: pointer; }
button:disabled { opacity: .55; cursor: default; }
nav button { flex: 1; }
nav button[aria-pressed="true"], .primary { background: var(--accent); color: var(--accent-text); font-weight: 650; }
.section-title { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.quiet, time { color: var(--muted); font-size: 13px; }
article { margin: 10px 0; padding: 14px; border-radius: 15px; background: var(--surface); }
article p { white-space: pre-wrap; overflow-wrap: anywhere; margin-top: 7px; }
article .question { font-weight: 650; }
.empty { color: var(--muted); }
form { display: grid; gap: 14px; margin-top: 15px; }
label { display: grid; gap: 5px; font-size: 13px; font-weight: 650; }
input, select, textarea { width: 100%; background: var(--surface); border: 1px solid transparent; border-radius: 11px; padding: 10px; font-size: 16px; font-weight: 400; }
textarea { resize: vertical; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }