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
+105
View File
@@ -0,0 +1,105 @@
import { createHmac, timingSafeEqual } from 'node:crypto';
import { readFileSync } from 'node:fs';
import { validateSettings } from './store.mjs';
const assets = new Map([
['/', ['index.html', 'text/html; charset=utf-8']],
['/app.js', ['app.js', 'text/javascript; charset=utf-8']],
['/style.css', ['style.css', 'text/css; charset=utf-8']],
].map(([route, [file, type]]) => [route, [readFileSync(new URL('./public/' + file, import.meta.url)), type]]));
const securityHeaders = {
'cache-control': 'no-store',
'x-content-type-options': 'nosniff',
'referrer-policy': 'no-referrer',
'content-security-policy': "default-src 'none'; script-src 'self' https://telegram.org; style-src 'self'; connect-src 'self'; img-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'self' https://web.telegram.org https://*.telegram.org",
};
export function verifyTelegram(initData, botToken, owner, now = Date.now()) {
if (!initData || initData.length > 8192 || !botToken || !owner) return false;
const params = new URLSearchParams(initData);
const entries = [...params.entries()];
if (new Set(params.keys()).size !== entries.length) return false;
const hash = params.get('hash') ?? '';
if (!/^[a-f0-9]{64}$/.test(hash)) return false;
const date = Number(params.get('auth_date'));
const age = now / 1000 - date;
if (!Number.isSafeInteger(date) || age < -60 || age > 3600) return false;
const secret = createHmac('sha256', 'WebAppData').update(botToken).digest();
const check = entries.filter(([key]) => key !== 'hash').sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)
.map(([key, value]) => key + '=' + value).join('\n');
const expected = createHmac('sha256', secret).update(check).digest();
if (!timingSafeEqual(expected, Buffer.from(hash, 'hex'))) return false;
try {
const { id } = JSON.parse(params.get('user'));
return Number.isSafeInteger(id) && String(id) === owner;
} catch {
return false;
}
}
function json(res, status, data) {
res.writeHead(status, { ...securityHeaders, 'content-type': 'application/json; charset=utf-8' });
res.end(JSON.stringify(data));
}
async function readSettings(req) {
const chunks = [];
let bytes = 0;
for await (const chunk of req.iterator({ destroyOnReturn: false })) {
bytes += chunk.length;
if (bytes > 20_000) {
req.resume();
return null;
}
chunks.push(chunk);
}
try {
return validateSettings(JSON.parse(Buffer.concat(chunks).toString()));
} catch {
return null;
}
}
export async function handleAdmin(req, res, store) {
const route = req.url?.split('?')[0];
const domain = process.env.ADMIN_HOST;
if (domain && req.headers.host !== domain) return false;
const asset = assets.get(route);
if (asset && req.method === 'GET') {
const [content, type] = asset;
res.writeHead(200, { ...securityHeaders, 'content-type': type });
res.end(content);
return true;
}
if (route !== '/api/admin' && route !== '/api/admin/settings' && route !== '/api/admin/history') return false;
const auth = req.headers.authorization ?? '';
if (!auth.startsWith('tma ') || !verifyTelegram(auth.slice(4), process.env.TELEGRAM_BOT_TOKEN, process.env.TELEGRAM_OWNER_ID)) {
json(res, 401, { error: 'telegram_auth' });
return true;
}
if (route === '/api/admin' && req.method === 'GET') {
json(res, 200, {
settings: store.getSettings(),
history: store.getHistory().reverse(),
});
} else if (route === '/api/admin/settings' && req.method === 'PUT') {
const settings = await readSettings(req);
if (settings) {
store.updateSettings(settings);
json(res, 200, { settings });
} else {
json(res, 400, { error: 'invalid_settings' });
}
} else if (route === '/api/admin/history' && req.method === 'DELETE') {
store.clearHistory();
json(res, 200, { history: [] });
} else {
json(res, 405, { error: 'method_not_allowed' });
}
return true;
}
+33
View File
@@ -0,0 +1,33 @@
import http from 'node:http';
import { timingSafeEqual } from 'node:crypto';
import { WebSocketServer } from 'ws';
import { openStore } from './store.mjs';
import { handleAdmin } from './admin.mjs';
import { handleTurn } from './turns.mjs';
const { DEVICE_TOKEN, ELEVENLABS_API_KEY, LLM_MODE = 'openrouter', OPENROUTER_API_KEY, PORT = '3000', HOST = '127.0.0.1' } = process.env;
if (!DEVICE_TOKEN || !ELEVENLABS_API_KEY || !['openrouter', 'echo'].includes(LLM_MODE) || (LLM_MODE === 'openrouter' && !OPENROUTER_API_KEY)) {
console.error('Missing required configuration or invalid LLM_MODE'); process.exit(1);
}
const store = openStore();
const server = http.createServer(async (req, res) => {
try {
if (req.method === 'GET' && req.url === '/healthz') { res.writeHead(200, { 'content-type': 'application/json', 'cache-control': 'no-store' }); res.end('{"ok":true}'); return; }
if (await handleAdmin(req, res, store)) return;
res.writeHead(404); res.end();
} catch { if (!res.headersSent) res.writeHead(500); res.end(); }
});
const sockets = new WebSocketServer({ noServer: true, maxPayload: 128 * 1024, perMessageDeflate: false });
let active = false;
server.on('upgrade', (req, socket, head) => {
const token = req.headers.authorization?.startsWith('Bearer ') ? req.headers.authorization.slice(7) : '';
const a = Buffer.from(token), b = Buffer.from(DEVICE_TOKEN);
if (req.url !== '/v1/voice' || a.length !== b.length || !timingSafeEqual(a, b)) { socket.end('HTTP/1.1 401 Unauthorized\r\nConnection: close\r\n\r\n'); return; }
if (active) { socket.end('HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\n\r\n'); return; }
sockets.handleUpgrade(req, socket, head, ws => {
active = true;
ws.once('close', () => { active = false; });
handleTurn(ws, store);
});
});
server.listen(Number(PORT), HOST, () => console.log(`Voice server listening on ${HOST}:${PORT}`));
+94
View File
@@ -0,0 +1,94 @@
import WebSocket from 'ws';
const env = process.env;
const sttUrl = env.ELEVEN_STT_URL || 'wss://api.elevenlabs.io/v1/speech-to-text/realtime';
const ttsUrl = env.ELEVEN_TTS_URL || 'wss://api.elevenlabs.io/v1/text-to-dialogue/stream-input';
const flashUrl = env.ELEVEN_FLASH_URL || 'wss://api.elevenlabs.io/v1/text-to-speech/{voice_id}/stream-input';
const llmUrl = env.OPENROUTER_URL || 'https://openrouter.ai/api/v1/chat/completions';
function endpoint(base, params) {
const url = new URL(base);
for (const [key, value] of Object.entries(params)) url.searchParams.set(key, value);
return url.toString();
}
function connect(url, signal) {
return new Promise((resolve, reject) => {
if (signal.aborted) return reject(new Error('aborted'));
const ws = new WebSocket(url, { headers: { 'xi-api-key': env.ELEVENLABS_API_KEY }, handshakeTimeout: 10000, maxPayload: 2 * 1024 * 1024 });
const abort = () => ws.terminate();
signal.addEventListener('abort', abort, { once: true });
ws.once('close', () => signal.removeEventListener('abort', abort));
ws.once('open', () => resolve(ws));
ws.once('error', reject);
});
}
export async function openStt(settings, signal, onPartial, onFinal, onFailure) {
const ws = await connect(endpoint(sttUrl, { model_id: settings.sttModel, audio_format: 'pcm_16000', language_code: 'ru', commit_strategy: 'manual' }), signal);
let final = false;
ws.on('message', data => {
try {
const msg = JSON.parse(data.toString());
if (msg.message_type === 'partial_transcript') onPartial(msg.text || '');
else if (msg.message_type === 'committed_transcript') { final = true; onFinal(msg.text || ''); }
else if (msg.error || /(?:error|rate_limited|auth_error|quota_exceeded)/.test(msg.message_type || '')) onFailure();
} catch { onFailure(); }
});
ws.on('error', onFailure);
ws.on('close', () => { if (!final && !signal.aborted) onFailure(); });
return ws;
}
export async function openTts(settings, signal, onAudio) {
const flash = settings.ttsModel === 'eleven_flash_v2_5';
const url = flash ? flashUrl.replace('{voice_id}', encodeURIComponent(settings.voiceId)) : ttsUrl;
const ws = await connect(endpoint(url, { model_id: settings.ttsModel, output_format: 'pcm_24000', ...(flash ? { auto_mode: 'true' } : { language_code: 'ru' }) }), signal);
let settled = false;
let resolveDone, rejectDone;
const done = new Promise((resolve, reject) => { resolveDone = resolve; rejectDone = reject; });
// The caller may still be generating text when the provider fails.
done.catch(() => {});
ws.on('message', data => {
try {
const msg = JSON.parse(data.toString());
if (msg.error || msg.type === 'error') throw new Error('tts_error');
if (msg.audio) onAudio(Buffer.from(msg.audio, 'base64'));
if (msg.is_final || msg.isFinal) { settled = true; resolveDone(); }
} catch { settled = true; rejectDone(new Error('tts_error')); }
});
ws.on('error', () => { if (!settled) { settled = true; rejectDone(new Error('tts_error')); } });
ws.on('close', () => { if (!settled) { settled = true; rejectDone(new Error('tts_closed')); } });
ws.send(JSON.stringify(flash ? { text: ' ' } : { voices: [settings.voiceId] }));
const keepalive = setInterval(() => { if (ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(flash ? { text: ' ' } : { keep_alive: true })); }, 10000);
ws.once('close', () => clearInterval(keepalive));
return { ws, done, text(text) { if (ws.readyState !== WebSocket.OPEN) throw new Error('tts_closed'); ws.send(JSON.stringify(flash ? { text } : { inputs: [{ text, voice_id: settings.voiceId }] })); }, finish() { if (ws.readyState !== WebSocket.OPEN) throw new Error('tts_closed'); ws.send(JSON.stringify(flash ? { text: '' } : { close_socket: true })); } };
}
export async function streamAnswer(settings, history, userText, signal, onText) {
if (env.LLM_MODE === 'echo') { onText(userText); return userText; }
if (!env.OPENROUTER_API_KEY) throw new Error('llm_config');
const messages = [{ role: 'system', content: settings.systemPrompt }, ...history.slice(-10).flatMap(t => [{ role: 'user', content: t.user }, { role: 'assistant', content: t.assistant }]), { role: 'user', content: userText }];
const response = await fetch(llmUrl, { method: 'POST', signal, headers: { Authorization: `Bearer ${env.OPENROUTER_API_KEY}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ model: settings.llmModel, messages, stream: true, max_tokens: 512 }) });
if (!response.ok || !response.body) throw new Error('llm_error');
let buffer = '', answer = '', finished = false;
const decoder = new TextDecoder();
const consume = event => {
const payload = event.split(/\r?\n/).filter(line => line.startsWith('data:')).map(line => line.slice(5).trimStart()).join('\n');
if (!payload) return;
if (payload === '[DONE]') { finished = true; return; }
const message = JSON.parse(payload);
if (message.error) throw new Error('llm_error');
const content = message.choices?.[0]?.delta?.content;
if (typeof content === 'string' && content) { answer += content; if (answer.length > 12000) throw new Error('llm_limit'); onText(content); }
};
for await (const chunk of response.body) {
buffer += decoder.decode(chunk, { stream: true });
if (buffer.length > 65536) throw new Error('llm_limit');
let match;
while ((match = /\r?\n\r?\n/.exec(buffer))) { consume(buffer.slice(0, match.index)); buffer = buffer.slice(match.index + match[0].length); }
}
buffer += decoder.decode();
if (buffer.trim()) consume(buffer);
if (!finished || !answer.trim()) throw new Error('llm_incomplete');
return answer;
}
+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; }
+62
View File
@@ -0,0 +1,62 @@
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
export const defaults = Object.freeze({
llmModel: 'anthropic/claude-haiku-5.5',
sttModel: 'scribe_v2_realtime',
ttsModel: 'eleven_v4_turbo',
voiceId: '21m00Tcm4TlvDq8ikWAM',
systemPrompt: 'Ты голосовой помощник Алиса. Отвечай по-русски, коротко и по делу. Пиши так, чтобы ответ хорошо звучал вслух: обычный текст, без Markdown. Если не знаешь ответа, скажи об этом прямо.',
});
export const ttsModels = ['eleven_v4_turbo', 'eleven_v3_conversational', 'eleven_flash_v2_5'];
export function validateSettings(input) {
if (typeof input !== 'object' || input === null) return null;
const { llmModel, sttModel, ttsModel, voiceId, systemPrompt } = input;
if (typeof llmModel !== 'string' || !/^[\w.~:-]+\/[\w.:~-]+$/.test(llmModel) || llmModel.length > 120) return null;
if (sttModel !== defaults.sttModel || !ttsModels.includes(ttsModel)) return null;
if (typeof voiceId !== 'string' || !/^[a-zA-Z0-9]{20}$/.test(voiceId)) return null;
if (typeof systemPrompt !== 'string' || !systemPrompt.trim() || systemPrompt.length > 4000) return null;
return { llmModel, sttModel, ttsModel, voiceId, systemPrompt: systemPrompt.trim() };
}
export function openStore(directory = process.env.DATA_DIR ?? './data') {
mkdirSync(directory, { recursive: true, mode: 0o700 });
const path = join(directory, 'assistant.json');
let state = { settings: { ...defaults }, history: [] };
try {
const saved = JSON.parse(readFileSync(path, 'utf8'));
const settings = validateSettings({ ...defaults, ...saved.settings });
if (!settings || !Array.isArray(saved.history)) throw new Error('Invalid assistant state');
state = { settings, history: saved.history };
} catch (error) {
if (error.code !== 'ENOENT') throw error;
}
function commit(next) {
const temporary = path + '.tmp';
writeFileSync(temporary, JSON.stringify(next) + '\n', { mode: 0o600 });
renameSync(temporary, path);
state = next;
}
return {
getSettings: () => ({ ...state.settings }),
getHistory: () => state.history.map(turn => ({ ...turn })),
updateSettings(settings) {
commit({ ...state, settings });
},
recordTurn(user, assistant) {
const turn = {
user: user.slice(0, 6000),
assistant: assistant.slice(0, 6000),
createdAt: new Date().toISOString(),
};
commit({ ...state, history: [...state.history.slice(-49), turn] });
},
clearHistory() {
commit({ ...state, history: [] });
},
};
}
+81
View File
@@ -0,0 +1,81 @@
import WebSocket from 'ws';
import { openStt, openTts, streamAnswer } from './providers.mjs';
const MAX_AUDIO = 60 * 16000 * 2;
const MAX_BUFFERED = 2 * 1024 * 1024;
export function handleTurn(client, store) {
const controller = new AbortController();
const { signal } = controller;
let state = 'waiting', stt, tts, pending, bytes = 0, received = 0, messages = 0, ended = false, terminal = false;
const timeout = setTimeout(() => fail('timeout'), 120000);
const send = obj => { if (client.readyState === WebSocket.OPEN) client.send(JSON.stringify(obj)); };
function cleanup() {
clearTimeout(timeout);
controller.abort();
if (stt && stt.readyState < WebSocket.CLOSING) stt.terminate();
if (tts?.ws && tts.ws.readyState < WebSocket.CLOSING) tts.ws.terminate();
}
function fail(code) {
if (terminal) return;
terminal = true; send({ type: 'error', code }); cleanup();
if (client.readyState === WebSocket.OPEN) client.close();
}
function sendChunk(chunk, commit = false) {
if (!stt || stt.readyState !== WebSocket.OPEN || stt.bufferedAmount > MAX_BUFFERED) return fail('stt_unavailable');
stt.send(JSON.stringify({ message_type: 'input_audio_chunk', audio_base_64: chunk.toString('base64'), ...(commit ? { commit: true } : {}) }));
}
async function respond(text, settings) {
if (state !== 'processing' || terminal) return;
text = text.trim();
if (!text) return fail('empty_transcript');
send({ type: 'transcript', text });
try {
tts = await openTts(settings, signal, audio => {
if (terminal || client.readyState !== WebSocket.OPEN) return;
if (client.bufferedAmount > MAX_BUFFERED) return fail('client_too_slow');
client.send(audio, { binary: true });
});
if (terminal) return;
const answer = await streamAnswer(settings, store.getHistory(), text, signal, part => tts.text(part));
tts.finish();
await tts.done;
if (terminal) return;
store.recordTurn(text, answer);
terminal = true; send({ type: 'done' }); cleanup();
if (client.readyState === WebSocket.OPEN) client.close(1000);
} catch { fail('upstream_error'); }
}
let settings;
let chain = Promise.resolve();
client.on('message', (data, binary) => {
if (++messages > 10000 || (received += data.length) > MAX_AUDIO + 65536) return fail('input_limit');
chain = chain.then(async () => {
if (terminal) return;
if (binary) {
if (state !== 'recording') return fail('invalid_state');
const audio = Buffer.from(data);
if (!audio.length || audio.length % 2 || (bytes += audio.length) > MAX_AUDIO) return fail('invalid_audio');
if (pending) sendChunk(pending);
pending = audio;
return;
}
let msg;
try { msg = JSON.parse(data.toString()); } catch { return fail('invalid_message'); }
if (msg?.type === 'start' && state === 'waiting') {
state = 'connecting'; settings = store.getSettings();
try {
stt = await openStt(settings, signal, text => { if (!terminal && state === 'recording') send({ type: 'partial', text }); }, text => { if (!ended || terminal) return; respond(text, settings); }, () => fail('stt_error'));
if (terminal) return;
state = 'recording';
} catch { fail('stt_error'); }
} else if (msg?.type === 'end' && state === 'recording') {
if (!pending) return fail('empty_audio');
ended = true; state = 'processing'; sendChunk(pending, true); pending = null;
} else fail('invalid_state');
}).catch(() => fail('internal_error'));
});
client.on('error', () => { if (!terminal) { terminal = true; cleanup(); } });
client.on('close', () => { if (!terminal) { terminal = true; cleanup(); } });
send({ type: 'ready' });
}