fix: full review pass (main process, voice pipeline, Hermes client, HUD) and e2e-validated local voice (v2.1.0.1)

Electron main
- utility-process channel: sherpa output copied into V8 buffers (Electron rejects external
  buffers), audio exchanged as base64; worker restart on timeout, identity-safe exit handling,
  deterministic native unload by restart
- webhook: loopback by default without secret, UTF-8-safe body assembly, clean restart, port validation
- files IPC: realpath-based root check, openPath allow-list (reveal-only outside document folders),
  Windows reserved names; store: debounced async atomic writes with backup of corrupt files;
  logger: streaming writes with rotation; log message size cap
- HTTP stream proxy: socket released on idle timeout / renderer destroyed, id validation
- model manager: inactivity timeout, retrying rm/rename (Windows locks), engine stopped before
  replacing a model; WAV decoder handles float/24-bit; IPC payload validation
- window: opaque rounded window on Windows, navigation lock-down, visibility events;
  Ctrl+Alt+Escape instead of the Task Manager shortcut; single-instance guard; EVEFLOW_USER_DATA

Voice pipeline
- abort semantics (SendHandle.aborted), abort before the stream opens, session id prefixes per transport
- hands-free re-arm after replies without speech, start/stop race, no chime on auto re-arm,
  no silence shipped to STT (400 ms pre-roll), no transcription of empty manual stops
- TTS: bounded prefetch, cancellable segments, non-interrupting notices, volume applied at play time
- SSE CRLF split, usage in chat completions, finish_reason length, phonetic regex hoisted

HUD
- core renderer: no canvas shadows, cached colours, reusable spectrum buffer, theme read on change,
  30 fps idle, stops when the window is hidden; ping flashes on send / tool / speech
- deltas coalesced per animation frame; stable auto-scroll; narrow selectors everywhere
- bundled fonts (offline), reduce-motion fix, error toasts, ops drawer below 1180 px,
  interim transcript and first-token latency in the core caption, Ctrl+K, dialog semantics,
  switch/aria roles, compact widget cleanup, Whisper small recommended for French
- docs/ROADMAP.md: audit results, e2e results and the plan towards a real JARVIS

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017Wn5VX9HNbJ7N54hR24u9Y
This commit is contained in:
Claude committed 2026-09-03 18:08:09 +00:00
1 parent b1b8d0d5d4
commit 5f0fa7a1e2
55 files changed
+1181 -433

No files matched your search

+69 -21
View File
@@ -1,7 +1,7 @@
/**
* Conversation orchestrator: sends user input to Hermes through the resolved transport,
* streams the answer into the chat store, drives the HUD state, speaks the reply and
* surfaces approvals / clarifications as pending requests.
* streams the answer into the chat store (coalesced per animation frame), drives the HUD
* state, speaks the reply and surfaces approvals / clarifications as pending requests.
*/
import type { HermesPushEvent } from '../../shared/ipc';
import { Log } from '../lib/log';
@@ -18,6 +18,34 @@ let activeMessageId: string | null = null;
const toolLabel = (name: string) => name.replace(/_/g, ' ');
// ── delta coalescing: one store update per animation frame ─────────────────
const pendingDelta = { id: '', content: '', reasoning: '', raf: 0 };
function flushDelta(): void {
if (pendingDelta.raf) cancelAnimationFrame(pendingDelta.raf);
const { id, content, reasoning } = pendingDelta;
pendingDelta.id = '';
pendingDelta.content = '';
pendingDelta.reasoning = '';
pendingDelta.raf = 0;
if (!id) return;
const chat = useChat.getState();
if (content) chat.appendToMessage(id, content);
if (reasoning) chat.appendToMessage(id, reasoning, 'reasoning');
}
function queueDelta(id: string, text: string, field: 'content' | 'reasoning'): void {
if (pendingDelta.id && pendingDelta.id !== id) flushDelta();
pendingDelta.id = id;
pendingDelta[field] += text;
if (!pendingDelta.raf) pendingDelta.raf = requestAnimationFrame(flushDelta);
}
/** Bumps a counter the core reads to flash briefly (send, tool start, speech start). */
export function pingCore(): void {
useChat.getState().ping();
}
function localToolContext() {
const chat = useChat.getState();
const settings = useSettings.getState().settings;
@@ -49,7 +77,7 @@ function localToolContext() {
};
}
function handleEvent(event: HermesStreamEvent, messageId: string, runIdRef: { id: string | null }): void {
function handleEvent(event: HermesStreamEvent, messageId: string, runIdRef: { id: string | null }, timing: { firstToken: number | null; startedAt: number }): void {
const chat = useChat.getState();
switch (event.kind) {
case 'run.started':
@@ -57,15 +85,20 @@ function handleEvent(event: HermesStreamEvent, messageId: string, runIdRef: { id
chat.setSending(true, event.runId);
break;
case 'delta':
chat.appendToMessage(messageId, event.text);
if (timing.firstToken === null) {
timing.firstToken = Date.now() - timing.startedAt;
chat.setLatency(timing.firstToken);
}
queueDelta(messageId, event.text, 'content');
speech.pushStream(event.text);
break;
case 'reasoning':
chat.appendToMessage(messageId, event.text, 'reasoning');
queueDelta(messageId, event.text, 'reasoning');
break;
case 'tool.start':
chat.pushActivity({ id: event.id, kind: 'tool', name: event.name, status: 'running', detail: event.args ? previewText(event.args, 160) : undefined });
chat.setHud('thinking');
pingCore();
break;
case 'tool.progress':
if (!chat.activity.some((a) => (event.id && a.id === event.id) || (a.name === event.name && a.status === 'running'))) {
@@ -113,6 +146,7 @@ function handleEvent(event: HermesStreamEvent, messageId: string, runIdRef: { id
break;
case 'completed':
if (event.usage) chat.updateMessage(messageId, { usage: event.usage });
if (event.status === 'length') chat.updateMessage(messageId, { status: 'cancelled' });
break;
case 'error':
chat.setError(event.message);
@@ -127,7 +161,8 @@ function addPending(request: Omit<PendingRequest, 'id' | 'createdAt'>): void {
const chat = useChat.getState();
chat.addPending(request);
chat.setHud('alert');
speech.say(request.kind === 'approval' ? 'Autorisation requise.' : request.kind === 'clarify' ? request.description : 'Saisie requise.');
// Spoken without interrupting the streamed answer.
speech.say(request.kind === 'approval' ? 'Autorisation requise.' : request.kind === 'clarify' ? request.description : 'Saisie requise.', { interrupt: false });
}
export async function sendMessage(text: string, images: string[] = [], source = 'eveflow'): Promise<void> {
@@ -139,6 +174,7 @@ export async function sendMessage(text: string, images: string[] = [], source =
speech.stop();
chat.setError(null);
chat.setLatency(null);
chat.addMessage({ role: 'user', content: trimmed || '(image)', images: images.length ? images : undefined, source, status: 'done' });
const history = chat.messages
.filter((m) => m.role !== 'system' && m.status !== 'error')
@@ -148,7 +184,9 @@ export async function sendMessage(text: string, images: string[] = [], source =
activeMessageId = messageId;
chat.setSending(true);
chat.setHud('thinking');
pingCore();
const startedAt = Date.now();
const timing = { firstToken: null as number | null, startedAt };
const runIdRef = { id: null as string | null };
const client = hermes.client();
@@ -158,7 +196,7 @@ export async function sendMessage(text: string, images: string[] = [], source =
images,
history,
sessionId: settings.hermesSessionId,
onEvent: (event) => handleEvent(event, messageId, runIdRef),
onEvent: (event) => handleEvent(event, messageId, runIdRef, timing),
localToolDefinitions: LOCAL_TOOL_DEFINITIONS,
localToolExecutor: (name, args) => executeLocalTool(name, args, localToolContext())
},
@@ -169,20 +207,26 @@ export async function sendMessage(text: string, images: string[] = [], source =
try {
const reply = await handle.result;
const current = useChat.getState().messages.find((m) => m.id === messageId);
const finalText = reply || current?.content || '';
useChat.getState().updateMessage(messageId, { content: finalText, status: 'done' });
// Anything not streamed (non-delta transports) is spoken now.
if (settings.speech.autoSpeak) {
if (!current?.content && finalText) speech.say(finalText);
flushDelta();
const state = useChat.getState();
const current = state.messages.find((m) => m.id === messageId);
const streamedText = current?.content ?? '';
const finalText = handle.aborted ? streamedText : reply || streamedText;
const truncated = current?.status === 'cancelled';
state.updateMessage(messageId, { content: finalText || (handle.aborted ? '(interrompu)' : ''), status: handle.aborted || truncated ? 'cancelled' : 'done' });
if (handle.aborted) {
speech.discardStream();
} else if (settings.speech.autoSpeak) {
if (!streamedText && finalText) speech.say(finalText);
else speech.endStream();
} else {
speech.discardStream();
}
useChat.getState().setHud(speech.isSpeaking() ? 'speaking' : 'idle');
if (useHermes.getState().link === 'offline') useHermes.getState().connect().catch(() => undefined);
Log.info('hermes', `reply in ${Date.now() - startedAt} ms via ${handle.transport}`, { chars: finalText.length });
state.setHud(speech.isSpeaking() ? 'speaking' : 'idle');
if (!handle.aborted && useHermes.getState().link === 'offline') useHermes.getState().connect().catch(() => undefined);
Log.info('hermes', `${handle.aborted ? 'aborted' : 'reply'} in ${Date.now() - startedAt} ms via ${handle.transport}`, { chars: finalText.length, firstToken: timing.firstToken });
} catch (err) {
flushDelta();
const message = (err as Error).message || String(err);
const partial = useChat.getState().messages.find((m) => m.id === messageId)?.content ?? '';
useChat.getState().updateMessage(messageId, {
@@ -200,10 +244,14 @@ export async function sendMessage(text: string, images: string[] = [], source =
} finally {
active = null;
activeMessageId = null;
useChat.getState().setSending(false);
for (const a of useChat.getState().activity) {
if (a.status === 'running') useChat.getState().finishActivity({ id: a.id }, { status: 'done' });
const state = useChat.getState();
state.setSending(false);
state.setLatency(null);
for (const a of state.activity) {
if (a.status === 'running') state.finishActivity({ id: a.id }, { status: 'done' });
}
// Requests bound to this run cannot be answered once it is over.
for (const p of state.pending) if (!runIdRef.id || p.runId === runIdRef.id) state.removePending(p.id);
}
}
@@ -235,14 +283,13 @@ export async function resolvePending(request: PendingRequest, decision: string):
chat.removePending(request.id);
const client = useHermes.getState().client();
try {
if (!request.runId) throw new Error('run inconnu');
if (request.kind === 'approval') {
const choice = (['once', 'session', 'always', 'deny'].includes(decision) ? decision : decision === 'approve' ? 'once' : 'deny') as 'once' | 'session' | 'always' | 'deny';
if (!request.runId) throw new Error('run inconnu');
await client.approveRun(request.runId, choice, request.requestId || undefined);
chat.addMessage({ role: 'system', content: `Autorisation ${choice === 'deny' ? 'refusée' : 'accordée'}${request.tool ? ` (${toolLabel(request.tool)})` : ''}.`, status: 'done' });
} else {
// Clarifications and secrets are answered by steering the run with the reply.
if (!request.runId) throw new Error('run inconnu');
await client.steerRun(request.runId, decision);
chat.addMessage({ role: 'system', content: request.secret ? 'Saisie transmise à Hermes.' : `Réponse transmise : ${decision}`, status: 'done' });
}
@@ -278,6 +325,7 @@ export function handlePush(event: HermesPushEvent): void {
speech.say(event.jobName ? `Résultat de ${event.jobName}. ${event.text}` : event.text);
}
chat.setHud(event.status?.includes('fail') ? 'alert' : 'success');
pingCore();
setTimeout(() => {
const s = useChat.getState();
if (s.hud === 'success' || s.hud === 'alert') s.setHud(speech.isSpeaking() ? 'speaking' : 'idle');
+45 -20
View File
@@ -261,6 +261,7 @@ export class HermesClient {
for (const event of normalizeLifecycleEvent(eventName, data)) onEvent(event);
});
const errorChunks: string[] = [];
let handleReady = false;
const handle = await httpStream(
{
url: `${this.base}${path}`,
@@ -271,7 +272,6 @@ export class HermesClient {
},
{ onChunk: (text) => (handleReady ? parser.feed(text) : errorChunks.push(text)) }
);
let handleReady = handle.start.ok;
if (!handle.start.ok) {
await handle.done.catch(() => undefined);
throw new HttpError(handle.start.status, errorMessage(handle.start.status, errorChunks.join('')));
@@ -285,25 +285,45 @@ export class HermesClient {
send(options: SendOptions, transport: ResolvedTransport): SendHandle {
const effective: ResolvedTransport = options.images?.length && transport === 'runs' ? 'completions' : transport;
let abort: () => void = () => undefined;
let aborted = false;
let abortImpl: () => void = () => undefined;
const setAbort = (fn: () => void) => {
abortImpl = fn;
if (aborted) fn();
};
const isAborted = () => aborted;
const result =
effective === 'runs'
? this.sendViaRuns(options, (fn) => (abort = fn))
? this.sendViaRuns(options, setAbort, isAborted)
: effective === 'sessions'
? this.sendViaSessions(options, (fn) => (abort = fn))
: this.sendViaCompletions(options, (fn) => (abort = fn));
return { result, abort: () => abort(), transport: effective };
? this.sendViaSessions(options, setAbort, isAborted)
: this.sendViaCompletions(options, setAbort, isAborted);
return {
result,
transport: effective,
get aborted() {
return aborted;
},
abort: () => {
aborted = true;
abortImpl();
}
};
}
private async sendViaRuns(options: SendOptions, setAbort: (fn: () => void) => void): Promise<string> {
private async sendViaRuns(options: SendOptions, setAbort: (fn: () => void) => void, isAborted: () => boolean): Promise<string> {
const { onEvent } = options;
const run = await this.startRun({
input: options.text,
session_id: options.sessionId || undefined,
session_id: plainSession(options.sessionId) || undefined,
instructions: this.config.instructions || undefined,
model: this.config.model || undefined
});
const runId = run.run_id;
if (isAborted()) {
this.stopRun(runId).catch(() => undefined);
return '';
}
onEvent({ kind: 'run.started', runId });
Log.info('hermes', `run ${runId} started`);
@@ -329,7 +349,7 @@ export class HermesClient {
});
await handle.done;
if (stopped) return streamedText;
if (stopped || isAborted()) return streamedText;
if (failure) throw new Error(failure);
if (!completed) {
@@ -349,7 +369,7 @@ export class HermesClient {
return finalText || streamedText;
}
private async sendViaSessions(options: SendOptions, setAbort: (fn: () => void) => void): Promise<string> {
private async sendViaSessions(options: SendOptions, setAbort: (fn: () => void) => void, isAborted: () => boolean): Promise<string> {
const { onEvent } = options;
let sessionId = options.sessionId;
if (!sessionId || !sessionId.startsWith('hs:')) {
@@ -357,6 +377,7 @@ export class HermesClient {
sessionId = `hs:${session.id}`;
onEvent({ kind: 'session', sessionId });
}
if (isAborted()) return '';
const realId = sessionId.slice(3);
const body: Rec = { input: options.text };
if (this.config.instructions) body.instructions = this.config.instructions;
@@ -374,11 +395,12 @@ export class HermesClient {
});
setAbort(() => handle.abort());
await handle.done;
if (isAborted()) return streamed;
if (failure) throw new Error(failure);
return finalText || streamed;
}
private async sendViaCompletions(options: SendOptions, setAbort: (fn: () => void) => void): Promise<string> {
private async sendViaCompletions(options: SendOptions, setAbort: (fn: () => void) => void, isAborted: () => boolean): Promise<string> {
const { onEvent } = options;
const useTools = this.config.localTools && !!options.localToolExecutor && !!options.localToolDefinitions?.length;
const messages: HermesChatMessage[] = [];
@@ -389,16 +411,13 @@ export class HermesClient {
: options.text;
messages.push({ role: 'user', content: userContent });
let aborted = false;
let currentHandle: StreamHandle | null = null;
setAbort(() => {
aborted = true;
currentHandle?.abort();
});
setAbort(() => currentHandle?.abort());
const aborted = () => isAborted();
let fullText = '';
let toolsAllowed = useTools;
for (let iteration = 0; iteration < 6 && !aborted; iteration++) {
for (let iteration = 0; iteration < 6 && !aborted(); iteration++) {
const payload: Rec = { model: this.config.model || 'hermes-agent', messages, stream: true };
if (toolsAllowed) {
payload.tools = options.localToolDefinitions;
@@ -428,7 +447,8 @@ export class HermesClient {
});
const headers = this.headers({ Accept: 'text/event-stream' });
if (options.sessionId) headers['X-Hermes-Session-Id'] = options.sessionId;
const sessionId = plainSession(options.sessionId);
if (sessionId) headers['X-Hermes-Session-Id'] = sessionId;
const handle = await httpStream(
{ url: `${this.base}/v1/chat/completions`, method: 'POST', headers, body: JSON.stringify(payload), timeoutMs: 10 * 60_000 },
{ onChunk: (text) => (ready ? parser.feed(text) : errorChunks.push(text)) }
@@ -454,7 +474,7 @@ export class HermesClient {
parser.end();
fullText += iterationText;
if (finishReason === 'tool_calls' && toolsAllowed && toolCalls.size > 0 && !aborted) {
if (finishReason === 'tool_calls' && toolsAllowed && toolCalls.size > 0 && !aborted()) {
const calls = [...toolCalls.values()];
messages.push({
role: 'assistant',
@@ -471,11 +491,16 @@ export class HermesClient {
}
break;
}
onEvent({ kind: 'completed', status: aborted ? 'cancelled' : 'completed', text: fullText });
onEvent({ kind: 'completed', status: aborted() ? 'cancelled' : 'completed', text: fullText });
return fullText;
}
}
/** Session ids created by the sessions transport carry an `hs:` prefix; other endpoints get the bare id. */
export function plainSession(id: string): string {
return id.startsWith('hs:') ? id.slice(3) : id;
}
export function normalizeJob(job: HermesJob): HermesJob {
const raw = job as Rec;
const id = String(raw.id ?? raw.job_id ?? raw.name ?? '');
+1
View File
@@ -212,6 +212,7 @@ export function normalizeCompletionChunk(
): { events: HermesStreamEvent[]; finishReason: string | null } {
const events: HermesStreamEvent[] = [];
if (!isRec(chunk)) return { events, finishReason: null };
if (isRec(chunk.usage)) events.push({ kind: 'completed', status: 'usage', usage: chunk.usage as HermesUsage });
const choice = Array.isArray(chunk.choices) && isRec(chunk.choices[0]) ? (chunk.choices[0] as Rec) : null;
if (!choice) return { events, finishReason: null };
const delta = isRec(choice.delta) ? (choice.delta as Rec) : isRec(choice.message) ? (choice.message as Rec) : null;
+3 -1
View File
@@ -167,8 +167,10 @@ export interface SendOptions {
}
export interface SendHandle {
/** Resolves with the final assistant text. */
/** Resolves with the final assistant text (partial text when aborted). */
result: Promise<string>;
abort: () => void;
/** True once abort() was called; the caller must not treat the result as a completed reply. */
readonly aborted: boolean;
transport: Exclude<HermesTransport, 'auto'>;
}
-70
View File
@@ -1,70 +0,0 @@
/**
* LogService — Centralized structured logger for EveFlow.
*
* In Electron (production/dev): sends log entries via window.electronAPI.log()
* which the main process writes to %APPDATA%/EveFlow/eveflow.log
*
* Fallback (browser only): console only.
*/
export type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR';
export interface LogEntry {
ts: string; // ISO timestamp
level: LogLevel;
tag: string; // Component / service tag (e.g. "HermesAgent", "Minimax")
message: string;
data?: unknown; // Any extra serializable payload
}
export class LogService {
private static readonly MAX_CONSOLE_DATA_LEN = 2000;
// Core write method
public static log(level: LogLevel, tag: string, message: string, data?: unknown): void {
const entry: LogEntry = {
ts: new Date().toISOString(),
level,
tag,
message,
data
};
// Always write to console for DevTools visibility
const consoleMsg = `[${entry.ts}] [${level}] [${tag}] ${message}`;
const dataStr = data !== undefined ? JSON.stringify(data, null, 2) : '';
switch (level) {
case 'ERROR': console.error(consoleMsg, dataStr ? dataStr.slice(0, this.MAX_CONSOLE_DATA_LEN) : ''); break;
case 'WARN': console.warn(consoleMsg, dataStr ? dataStr.slice(0, this.MAX_CONSOLE_DATA_LEN) : ''); break;
case 'DEBUG': console.debug(consoleMsg, dataStr ? dataStr.slice(0, this.MAX_CONSOLE_DATA_LEN) : ''); break;
default: console.info(consoleMsg, dataStr ? dataStr.slice(0, this.MAX_CONSOLE_DATA_LEN) : ''); break;
}
// Send to Electron main process for file persistence
try {
const electronAPI = (window as any).electronAPI;
if (electronAPI?.writeLog) {
electronAPI.writeLog(entry);
}
} catch {
// Silently ignore: browser context has no electronAPI
}
}
public static debug(tag: string, message: string, data?: unknown): void {
this.log('DEBUG', tag, message, data);
}
public static info(tag: string, message: string, data?: unknown): void {
this.log('INFO', tag, message, data);
}
public static warn(tag: string, message: string, data?: unknown): void {
this.log('WARN', tag, message, data);
}
public static error(tag: string, message: string, data?: unknown): void {
this.log('ERROR', tag, message, data);
}
}
+2 -2
View File
@@ -71,9 +71,9 @@ class AudioBus {
}
/** Normalised output spectrum (0..1) with `bins` values, low to high frequency. */
spectrum(bins: number, source: 'output' | 'input' = 'output'): Float32Array {
spectrum(bins: number, source: 'output' | 'input' = 'output', out: Float32Array = new Float32Array(bins)): Float32Array {
const analyser = source === 'output' ? this.outputAnalyser : this.inputAnalyser;
const out = new Float32Array(bins);
out.fill(0);
if (!analyser) {
if (this.synthetic > 0) {
const t = performance.now() / 1000;
+12 -1
View File
@@ -88,6 +88,7 @@ export class MicCapture {
private active = false;
private options: CaptureOptions | null = null;
private sampleRate = 16_000;
private hadSpeech = false;
get isActive(): boolean {
return this.active;
@@ -98,6 +99,7 @@ export class MicCapture {
this.options = options;
this.chunks = [];
this.totalSamples = 0;
this.hadSpeech = false;
this.vad = options.mode === 'auto' ? new EnergyVad(options.vad ?? DEFAULT_VAD) : null;
if (!navigator.mediaDevices?.getUserMedia) {
@@ -169,6 +171,12 @@ export class MicCapture {
if (!this.active || !this.options) return;
this.chunks.push(samples);
this.totalSamples += samples.length;
// Before speech starts only a short pre-roll (~400 ms) is kept, so silence is never shipped to STT.
if (this.vad && !this.hadSpeech) {
while (this.chunks.length > 1 && this.totalSamples - this.chunks[0].length > this.sampleRate * 0.4) {
this.totalSamples -= this.chunks.shift()!.length;
}
}
const level = rms(samples);
this.options.callbacks.onLevel?.(Math.min(1, level * 6));
@@ -184,6 +192,7 @@ export class MicCapture {
private handleVad(signal: VadSignal): void {
switch (signal) {
case 'speech-start':
this.hadSpeech = true;
this.options?.callbacks.onSpeechStart?.();
break;
case 'speech-end':
@@ -222,10 +231,12 @@ export class MicCapture {
/** Stop capture. In manual mode this finalises the utterance; otherwise reports the reason. */
stop(reason: 'stopped' | 'no-speech' | 'too-short' = 'stopped'): void {
if (!this.active) return;
if (reason === 'stopped') {
// Manual stop: transcribe only if something was actually said (or no VAD is running).
if (reason === 'stopped' && (!this.vad || this.hadSpeech)) {
this.finishUtterance('stopped');
return;
}
if (reason === 'stopped') reason = 'no-speech';
const opts = this.options;
this.teardown();
opts?.callbacks.onEnd(reason);
+3 -2
View File
@@ -35,9 +35,10 @@ class SpeechFacade {
return this.engine?.isActive ?? false;
}
say(text: string): void {
say(text: string, options: { interrupt?: boolean } = {}): void {
if (useSettings.getState().settings.speech.provider === 'off') return;
this.tts.speak(text);
// While an answer streams, spoken notices are inserted without discarding the rest.
this.tts.speak(text, { interrupt: options.interrupt ?? !this.streaming });
}
pushStream(delta: string): void {
+62 -36
View File
@@ -1,7 +1,7 @@
/**
* Text-to-speech engine with a sentence queue, prefetching and Web Audio playback so the HUD
* reacts to the actual waveform. Providers: OpenAI-compatible /v1/audio/speech, system voices,
* and the legacy Google Translate endpoint (no key, online only).
* Text-to-speech engine with a sentence queue, bounded prefetching and Web Audio playback so
* the HUD reacts to the actual waveform. Providers: in-app sherpa-onnx (Kokoro / Piper),
* OpenAI-compatible /v1/audio/speech, system voices, and the legacy Google Translate endpoint.
*/
import { Log } from '../../lib/log';
import { bridge } from '../../lib/bridge';
@@ -35,6 +35,8 @@ interface QueueItem {
audio?: Promise<AudioBuffer | null>;
}
const LOOKAHEAD = 2;
export function ttsEndpoint(apiUrl: string): string {
const base = apiUrl.trim().replace(/\/+$/, '');
if (base.endsWith('/speech')) return base;
@@ -46,8 +48,7 @@ export function ttsEndpoint(apiUrl: string): string {
export class TtsEngine {
private queue: QueueItem[] = [];
private playing = false;
private currentSource: AudioBufferSourceNode | null = null;
private currentUtterance: SpeechSynthesisUtterance | null = null;
private cancelCurrent: (() => void) | null = null;
private streamBuffer = '';
private generation = 0;
private config: TtsConfig;
@@ -60,7 +61,6 @@ export class TtsEngine {
updateConfig(config: TtsConfig): void {
this.config = config;
audioBus.setVolume(config.volume);
}
onState(listener: (state: TtsState) => void): () => void {
@@ -78,10 +78,18 @@ export class TtsEngine {
for (const l of this.listeners) l(state);
}
/** Speak a finished text (clears any pending speech first). */
speak(text: string): void {
this.stop();
for (const chunk of chunkForSpeech(text)) this.enqueue(chunk);
/**
* Speak a finished text. With `interrupt` (default) pending speech is discarded first;
* without it the text is inserted ahead of the queue and streamed speech resumes afterwards.
*/
speak(text: string, options: { interrupt?: boolean } = {}): void {
const interrupt = options.interrupt ?? true;
if (interrupt) this.stop();
const items = chunkForSpeech(text).map((chunk) => this.makeItem(chunk)).filter((i): i is QueueItem => !!i);
if (interrupt) this.queue.push(...items);
else this.queue.unshift(...items);
this.fillPrefetch();
void this.drain();
}
/** Feed streamed tokens; complete sentences are spoken as soon as they are available. */
@@ -100,32 +108,32 @@ export class TtsEngine {
}
enqueue(text: string): void {
if (this.config.provider === 'off') return;
const clean = cleanForSpeech(text);
if (!clean || !/[\p{L}\p{N}]/u.test(clean)) return;
const item: QueueItem = { text: clean };
if (this.config.provider !== 'system') item.audio = this.prefetch(clean, this.generation);
const item = this.makeItem(text);
if (!item) return;
this.queue.push(item);
this.fillPrefetch();
void this.drain();
}
private makeItem(text: string): QueueItem | null {
if (this.config.provider === 'off') return null;
const clean = cleanForSpeech(text);
if (!clean || !/[\p{L}\p{N}]/u.test(clean)) return null;
return { text: clean };
}
/** Keep at most LOOKAHEAD synthesis requests in flight ahead of playback. */
private fillPrefetch(): void {
if (this.config.provider === 'system') return;
for (const item of this.queue.slice(0, LOOKAHEAD)) item.audio ??= this.prefetch(item.text, this.generation);
}
stop(): void {
this.generation++;
this.queue = [];
this.streamBuffer = '';
if (this.currentSource) {
try {
this.currentSource.onended = null;
this.currentSource.stop();
} catch {
/* already stopped */
}
this.currentSource = null;
}
if (this.currentUtterance) {
this.currentUtterance.onend = null;
this.currentUtterance = null;
}
this.cancelCurrent?.();
this.cancelCurrent = null;
if (typeof speechSynthesis !== 'undefined') speechSynthesis.cancel();
audioBus.setSyntheticLevel(0);
this.playing = false;
@@ -138,15 +146,17 @@ export class TtsEngine {
const gen = this.generation;
while (this.queue.length > 0 && gen === this.generation) {
const item = this.queue.shift()!;
this.fillPrefetch();
try {
if (item.audio) {
if (this.config.provider !== 'system') {
this.setState('loading');
const buffer = await item.audio;
const buffer = await (item.audio ?? this.prefetch(item.text, gen));
if (gen !== this.generation) break;
if (buffer) {
this.setState('speaking');
await this.playBuffer(buffer, gen);
} else {
this.setState('speaking');
await this.speakSystem(item.text, gen);
}
} else {
@@ -174,7 +184,7 @@ export class TtsEngine {
return audioBus.context.decodeAudioData(copy);
})
.catch((err) => {
Log.warn('tts', `synthesis failed, falling back to system voice: ${(err as Error).message}`);
if (gen === this.generation) Log.warn('tts', `synthesis failed, falling back to system voice: ${(err as Error).message}`);
return null;
});
}
@@ -238,15 +248,26 @@ export class TtsEngine {
return new Promise((resolve) => {
if (gen !== this.generation) return resolve();
const ctx = audioBus.context;
audioBus.setVolume(this.config.volume);
const source = ctx.createBufferSource();
source.buffer = buffer;
if (this.config.provider === 'google-free') source.playbackRate.value = Math.max(0.5, Math.min(2, this.config.speed || 1));
source.connect(audioBus.output);
source.onended = () => {
if (this.currentSource === source) this.currentSource = null;
const finish = () => {
if (this.cancelCurrent === cancel) this.cancelCurrent = null;
resolve();
};
this.currentSource = source;
const cancel = () => {
source.onended = null;
try {
source.stop();
} catch {
/* already stopped */
}
finish();
};
source.onended = finish;
this.cancelCurrent = cancel;
source.start();
});
}
@@ -268,12 +289,17 @@ export class TtsEngine {
if (pulse) clearInterval(pulse);
pulse = null;
audioBus.setSyntheticLevel(0);
if (this.currentUtterance === utterance) this.currentUtterance = null;
if (this.cancelCurrent === cancel) this.cancelCurrent = null;
resolve();
};
const cancel = () => {
utterance.onend = null;
utterance.onerror = null;
finish();
};
utterance.onend = finish;
utterance.onerror = finish;
this.currentUtterance = utterance;
this.cancelCurrent = cancel;
speechSynthesis.speak(utterance);
});
}
+35 -8
View File
@@ -51,6 +51,7 @@ class VoiceController {
private browser = new BrowserRecognizer();
private handsFreeTimer: ReturnType<typeof setTimeout> | null = null;
private stopping = false;
private startSeq = 0;
private unsubscribeVoice: (() => void) | null = null;
init(): void {
@@ -61,6 +62,13 @@ class VoiceController {
this.scheduleHandsFree(350);
}
});
// Hands-free also re-arms when a reply ends without speech (voice off, empty answer, error).
useChat.subscribe((state, prev) => {
if (prev.isSending && !state.isSending) {
const v = useVoice.getState();
if (v.handsFree && v.phase === 'off' && !speech.isSpeaking()) this.scheduleHandsFree(400);
}
});
}
dispose(): void {
@@ -83,6 +91,7 @@ class VoiceController {
if (on && !this.isListening && !speech.isSpeaking()) void this.start();
if (!on) {
this.clearHandsFreeTimer();
if (this.isListening) this.cancel();
}
}
@@ -90,7 +99,8 @@ class VoiceController {
this.clearHandsFreeTimer();
this.handsFreeTimer = setTimeout(() => {
const v = useVoice.getState();
if (v.handsFree && v.phase === 'off' && !useChat.getState().isSending && !speech.isSpeaking()) void this.start();
const bargeIn = useSettings.getState().settings.voice.bargeIn;
if (v.handsFree && v.phase === 'off' && !useChat.getState().isSending && (bargeIn || !speech.isSpeaking())) void this.start(true);
}, delay);
}
@@ -99,14 +109,16 @@ class VoiceController {
this.handsFreeTimer = null;
}
async start(): Promise<void> {
/** @param auto true when re-armed by the hands-free loop (no chime, patient timeout). */
async start(auto = false): Promise<void> {
const voice = useVoice.getState();
if (voice.phase !== 'off') return;
const settings = useSettings.getState().settings.voice;
const seq = ++this.startSeq;
voice.setError(null);
voice.setInterim('');
if (!settings.bargeIn) speech.stop();
else speech.stop();
// Barge-in keeps the assistant talking while the mic opens (echo cancellation handles the overlap).
if (!(auto && settings.bargeIn)) speech.stop();
useChat.getState().setHud('listening');
voice.setPhase('arming');
@@ -125,18 +137,29 @@ class VoiceController {
silenceMs: settings.silenceMs,
speechRatio: SENSITIVITY_RATIO[sensitivity],
minRms: SENSITIVITY_MIN_RMS[sensitivity],
noSpeechTimeoutMs: useVoice.getState().handsFree ? 20_000 : 9_000
noSpeechTimeoutMs: useVoice.getState().handsFree ? Number.POSITIVE_INFINITY : 9_000
},
callbacks: {
onLevel: (level) => useVoice.getState().setInputLevel(level),
onSpeechStart: () => useVoice.getState().setPhase('speech'),
onLevel: (level) => {
const q = Math.round(level * 20) / 20;
if (q !== useVoice.getState().inputLevel) useVoice.getState().setInputLevel(q);
},
onSpeechStart: () => {
useVoice.getState().setPhase('speech');
useChat.getState().ping();
},
onUtterance: (wav) => void this.transcribe(wav),
onEnd: (reason) => this.onCaptureEnd(reason),
onError: (message) => useVoice.getState().setError(message)
}
});
if (seq !== this.startSeq || useVoice.getState().phase !== 'arming') {
// stop() was called while the microphone was opening.
this.capture.cancel();
return;
}
useVoice.getState().setPhase('listening');
this.playChime(true);
if (!auto) this.playChime(true);
} catch (err) {
const message = (err as Error).message;
useVoice.getState().setError(message);
@@ -166,6 +189,7 @@ class VoiceController {
stop(): void {
this.stopping = true;
this.startSeq++;
this.clearHandsFreeTimer();
if (useSettings.getState().settings.voice.provider === 'browser') this.browser.stop();
else if (this.capture.isActive) this.capture.stop('stopped');
@@ -174,6 +198,7 @@ class VoiceController {
}
cancel(): void {
this.startSeq++;
this.clearHandsFreeTimer();
this.browser.stop();
this.capture.cancel();
@@ -224,6 +249,7 @@ class VoiceController {
this.attentionUntil = 0;
if (text.trim()) {
await sendMessage(text, [], 'voice');
if (useVoice.getState().handsFree && !speech.isSpeaking()) this.scheduleHandsFree(350);
} else {
voice.setError('Transcription vide.');
useChat.getState().setHud('idle');
@@ -237,6 +263,7 @@ class VoiceController {
useChat.getState().setHud('error');
Log.error('voice', `transcription failed: ${message}`);
setTimeout(() => useChat.getState().setHud('idle'), 3000);
if (useVoice.getState().handsFree) this.scheduleHandsFree(3000);
}
}