Tighten wake detection and keep short commands quiet
This commit is contained in:
+35
-9
@@ -1,15 +1,19 @@
|
||||
import WebSocket from 'ws';
|
||||
import { openStt, openTts, streamAnswer } from './providers.mjs';
|
||||
import { deviceTools } from './device.mjs';
|
||||
import { quickAction } from './quick-actions.mjs';
|
||||
|
||||
const MAX_AUDIO = 60 * 16000 * 2;
|
||||
const STT_CHUNK_BYTES = 3200; // 100 ms of mono PCM16 at 16 kHz.
|
||||
const MAX_BUFFERED = 2 * 1024 * 1024;
|
||||
|
||||
export function handleTurn(client, store) {
|
||||
const controller = new AbortController();
|
||||
const { signal } = controller;
|
||||
const device = deviceTools(client, signal);
|
||||
let state = 'waiting', stt, tts, pending, bytes = 0, received = 0, messages = 0, ended = false, terminal = false;
|
||||
let state = 'waiting', stt, tts, bytes = 0, received = 0, messages = 0, ended = false, terminal = false;
|
||||
let pending = [];
|
||||
let pendingBytes = 0;
|
||||
const timeout = setTimeout(() => fail('timeout'), 120000);
|
||||
const send = obj => { if (client.readyState === WebSocket.OPEN) client.send(JSON.stringify(obj)); };
|
||||
function cleanup() {
|
||||
@@ -27,6 +31,14 @@ export function handleTurn(client, store) {
|
||||
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 } : {}) }));
|
||||
}
|
||||
function complete(text, answer) {
|
||||
store.recordTurn(text, answer);
|
||||
terminal = true;
|
||||
send({ type: 'done' });
|
||||
cleanup();
|
||||
if (client.readyState === WebSocket.OPEN) client.close(1000);
|
||||
}
|
||||
|
||||
async function respond(text, settings) {
|
||||
if (state !== 'processing' || terminal) return;
|
||||
state = 'responding';
|
||||
@@ -34,19 +46,24 @@ export function handleTurn(client, store) {
|
||||
if (!text) return fail('empty_transcript');
|
||||
send({ type: 'transcript', text });
|
||||
try {
|
||||
const action = quickAction(text);
|
||||
if (action) {
|
||||
const result = await device.execute(action.name, action.args).catch(() => null);
|
||||
if (terminal) return;
|
||||
if (action.success(result)) return complete(text, action.done);
|
||||
}
|
||||
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), device.execute);
|
||||
const answer = action?.failure ?? await streamAnswer(settings, store.getHistory(), text, signal, part => tts.text(part), device.execute);
|
||||
if (action) tts.text(answer);
|
||||
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);
|
||||
complete(text, answer);
|
||||
} catch { fail('upstream_error'); }
|
||||
}
|
||||
let settings;
|
||||
@@ -59,8 +76,13 @@ export function handleTurn(client, store) {
|
||||
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;
|
||||
if (pendingBytes >= STT_CHUNK_BYTES) {
|
||||
sendChunk(Buffer.concat(pending, pendingBytes));
|
||||
pending = [];
|
||||
pendingBytes = 0;
|
||||
}
|
||||
pending.push(audio);
|
||||
pendingBytes += audio.length;
|
||||
return;
|
||||
}
|
||||
let msg;
|
||||
@@ -74,8 +96,12 @@ export function handleTurn(client, store) {
|
||||
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;
|
||||
if (!pendingBytes) return fail('empty_audio');
|
||||
ended = true;
|
||||
state = 'processing';
|
||||
sendChunk(Buffer.concat(pending, pendingBytes), true);
|
||||
pending = [];
|
||||
pendingBytes = 0;
|
||||
} else fail('invalid_state');
|
||||
}).catch(() => fail('internal_error'));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user